在不同的编程语言中,绘制坐标系中的两个圆并判断它们的位置关系的方法有所不同。以下是几种常见编程语言中的示例代码:
Python 示例
使用 `matplotlib` 库绘制两个圆并判断它们的位置关系:
```python
import matplotlib.pyplot as plt
import math
输入两个圆的圆心坐标和半径
x1, y1, r1 = map(float, input("请输入第一个圆的圆心坐标和半径: ").split(','))
x2, y2, r2 = map(float, input("请输入第二个圆的圆心坐标和半径: ").split(','))
计算两个圆心之间的距离
d = math.sqrt((x1 - x2) 2 + (y1 - y2) 2)
判断两圆的位置关系
if d == r1 + r2:
print("两圆外切")
elif d == abs(r1 - r2):
print("两圆内切")
elif d < r1 + r2 and d > abs(r1 - r2):
print("两圆相交")
elif d > r1 + r2:
print("两圆相离")
else:
print("两圆重合")
绘制两个圆
fig, ax = plt.subplots()
circle1 = plt.Circle((x1, y1), r1, color='blue')
circle2 = plt.Circle((x2, y2), r2, color='red')
ax.add_artist(circle1)
ax.add_artist(circle2)
ax.set_xlim([min(x1, x2) - 1, max(x1, x2) + 1])
ax.set_ylim([min(y1, y2) - 1, max(y1, y2) + 1])
plt.show()
```
Java 示例
使用 Java 编写程序,提示用户输入两个圆的圆心坐标和半径,并判断它们的位置关系:
```java
import java.util.Scanner;
public class CirclePosition {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个圆的圆心坐标和半径: ");
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();
double r1 = sc.nextDouble();
System.out.print("请输入第二个圆的圆心坐标和半径: ");
double x2 = sc.nextDouble();
double y2 = sc.nextDouble();
double r2 = sc.nextDouble();
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (d == r1 + r2) {
System.out.println("两圆外切");
} else if (d == Math.abs(r1 - r2)) {
System.out.println("两圆内切");
} else if (d < r1 + r2 && d > Math.abs(r1 - r2)) {
System.out.println("两圆相交");
} else if (d > r1 + r2) {
System.out.println("两圆相离");
} else {
System.out.println("两圆重合");
}
}
}
```
C++ 示例
使用 C++ 编写程序,提示用户输入两个圆的圆心坐标和半径,并判断它们的位置关系: