如何在Java中检查两个给定的圆是否相切或相交?
圆是由一个在平面上移动的点形成的封闭图形,该点与给定点的距离保持不变。在本文中,我们将检查两个给定的圆是否相切或相交。
我们将得到两个圆,圆心1即(x1, y1)和圆心2即(x2,y2),以及半径R1和R2。我们需要检查给定的圆是否与另一个圆碰撞,因此会出现五种可能的情况,如下所述:
圆2位于圆1内部
圆1位于圆2内部
圆1和圆2相互相交
圆1和圆2相互接触
圆1和圆2没有重叠
现在,为了检查上述条件,我们将找到圆心1和圆心2之间的距离,并将其命名为“d”。
现在,
1. 如果d <= R1 – R2:圆2位于圆1内部
2. 如果d <= R2 – R1:圆1位于圆2内部
3. 如果d < R1 + R2:圆1和圆2相互相交
4. 如果d == R1 + R2:圆1和圆2相互接触
5. 否则,圆1和圆2没有重叠
“d”可以使用以下公式找到:
$$\mathrm{d\:=\:sqrt((x1\:–\:x2)^2\:+\:(y1\:–\:y2)^2}$$
让我们开始吧!
向您展示一些实例
实例-1
给定“d”的输入为:
圆心1 = (9, 3),圆心2 = (11, 1),R1 = 5 ,R2 = 4 。
找到“d”的值后,结果将为:
圆1和圆2相互相交
实例-2
给定“d”的输入为:
圆心1 = (5, 8),圆心2 = (9, 11),R1 = 20 ,R2 = 40 。
找到“d”的值后,结果将为:
圆1位于圆2内部
算法
步骤-1 − 声明并初始化变量。
步骤-2 − 找到圆的圆心1和圆心2之间的距离。
步骤-3 − 检查距离的五个条件。
步骤-4 − 打印结果。
多种方法
我们提供了不同方法的解决方案。
使用静态输入
使用用户定义的方法
让我们逐一查看程序及其输出。
方法-1:使用静态输入
在这种方法中,将分配半径1和半径2、圆心1和圆心2的值以找到“d”。然后根据算法,我们将找到直线是否接触、相交或位于圆之外。
示例
public class Main {
//main method
public static void main(String[] args){
//declaring variables
int x1 = 9, y1 = 3;
int x2 = 11, y2 = 1;
int r1 = 5, r2 = 4;
//finding d using the formula
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (d <= r1 - r2) {
//print if Circle 2 lie inside circle 1
System.out.println("Circle 2 lie inside circle 1");
}
else if (d <= r2 - r1) {
//print if Circle 1 lie inside 2
System.out.println("Circle 1 lie inside 2");
}
else if (d < r1 + r2) {
//print if Circle 1 and 2 intersect each other
System.out.println("Circle 1 and 2 intersect each other");
}
else if (d == r1 + r2) {
//print if Circle 1 and 2 touch each other
System.out.println("Circle 1 and 2 touch each other");
} else {
//print if Circle 1 and 2 do not touch each other
System.out.println("Circle 1 and 2 do not touch each other");
}
}
}
输出
Circle 1 and 2 intersect each other
方法-2:使用用户定义的方法
在这种方法中,将分配半径1和半径2、圆心1和圆心2的值以找到“d”。然后通过传递给定值来调用用户定义的方法,并根据算法,我们将找到直线是否接触、相交或位于圆之外。
示例
public class Main {
//main method
public static void main(String[] args){
//declaring variables
int x1 = 5, y1 = 8;
int x2 = 9, y2 = 11;
int r1 = 20, r2 = 40;
//calling user defined method
func(x1, y1, x2, y2, r1, r2);
}
//user defined method
static void func(int x1, int y1, int x2, int y2, int r1, int r2){
//finding d using the formula
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (d <= r1 - r2) {
//print if Circle 2 lie inside circle 1
System.out.println("Circle 2 lie inside circle 1");
}
else if (d <= r2 - r1) {
//print if Circle 1 lie inside 2
System.out.println("Circle 1 lie inside 2");
}
else if (d < r1 + r2) {
//print if Circle 1 and 2 intersect each other
System.out.println("Circle 1 and 2 intersect each other");
}
else if (d == r1 + r2) {
//print if Circle 1 and 2 touch each other
System.out.println("Circle 1 and 2 touch each other");
}
else {
//print if Circle 1 and 2 do not touch each other
System.out.println("Circle 1 and 2 do not touch each other");
}
}
}
输出
Circle 1 lie inside 2
在本文中,我们探讨了使用Java编程语言查找两个给定的圆是否相切或相交的不同方法。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP