首頁 > Java > java教程 > 主體

如何在Java中檢查兩個給定的圓是否相切或相交?

WBOY
發布: 2023-08-27 14:21:03
轉載
736 人瀏覽過

如何在Java中檢查兩個給定的圓是否相切或相交?

圓是透過追蹤在平面上移動的點而形成的閉合形狀,使得它與給定點的距離恆定。在本文中,我們將檢查兩個給定的圓是否相互接觸或相交。

我們將得到兩個圓,其中心為 1,即 (x1, y1),中心為 2,即 (x2,y2),半徑為 R1 和 R2。我們需要檢查給定的圓是否與另一個圓碰撞,因此會出現以下五種可能的情況 -

  • 圓 2 位於圓 1 內

  • #圓 1 位於圓 2 內

  • #圓 1 和圓 2 相交

  • 圓 1 和圓 2 互相接觸

  • 圓 1 和圓 2 不重疊

現在為了檢查上述條件,我們將找到中心 1 和中心 2 之間的距離,並將其命名為「d」。

現在,

  • 1。若 d

  • 2.如果 d

  • 3.如果 d

  • #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.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!