如何在Java中检查两个给定的圆是否相切或相交?

WBOY
WBOY 转载
2023-08-27 14:21:03 384浏览

如何在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 内

算法

  • Step-1 - 声明并初始化变量。

  • Step-2 - 找到圆的中心 1 和中心 2 之间的距离。

  • Step-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中检查两个给定的圆是否相切或相交?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除