בדוק אם שני עיגולים נתונים נוגעים או חותכים זה את זה

בדוק אם שני עיגולים נתונים נוגעים או חותכים זה את זה
נסה את זה ב-GfG Practice #practiceLinkDiv { display: none !חשוב; }

ישנם שני עיגולים A ו-B עם המרכזים שלהם C1(x1 y1) ו C2(x2 y2) ורדיוס R1 ו R2 . המשימה היא לבדוק ששני העיגולים A ו-B נוגעים זה בזה או לא.

דוגמאות:   

קלט: C1 = (3 4)
        C2 = (14 18)
        R1 = 5 R2 = 8
פלט: מעגלים לא נוגעים זה בזה.

קלט: C1 = (2 3)
        C2 = (15 28)
        R1 = 12 R2 = 10
פלט: מעגלים מצטלבים זה עם זה.

קלט: C1 = (-10 8)
        C2 = (14 -24)
        R1 = 30 R2 = 10

תרגול מומלץ בדוק אם שני עיגולים נתונים נוגעים זה בזה נסה זאת!

גִישָׁה:
המרחק בין המרכזים C1 ו-C2 מחושב כ

 C1C2 = sqrt((x1 - x2) 2+ (y1 - y2) 2 ).

נוצרים שלושה תנאים.

  1. אִם C1C2 <= R1 - R2: מעגל B נמצא בתוך A.
  2. אִם C1C2 <= R2 - R1: עיגול A נמצא בתוך B.
  3. אִם C1C2 < R1 + R2: מעגל חותך זה את זה.
  4. אִם C1C2 == R1 + R2: מעגל A ו-B נמצאים בקשר זה עם זה.
  5. אַחֶרֶת עיגול A ו-B אינם חופפים

להלן יישום הגישה לעיל: 

C++
   // C++ program to check if two   // circles touch each other or not.   #include          using     namespace     std  ;   int     circle  (  int     x1       int     y1       int     x2       int     y2       int     r1       int     r2  )   {      double     d     =     sqrt  ((  x1     -     x2  )     *     (  x1     -     x2  )      +     (  y1     -     y2  )     *     (  y1     -     y2  ));      if     (  d      <=     r1     -     r2  )     {      cout      < <     'Circle B is inside A'  ;      }      else     if     (  d      <=     r2     -     r1  )     {      cout      < <     'Circle A is inside B'  ;      }      else     if     (  d      <     r1     +     r2  )     {      cout      < <     'Circle intersect to each other'  ;      }      else     if     (  d     ==     r1     +     r2  )     {      cout      < <     'Circle touch to each other'  ;      }      else     {      cout      < <     'Circle not touch to each other'  ;      }   }   // Driver code   int     main  ()   {      int     x1     =     -10       y1     =     8  ;      int     x2     =     14       y2     =     -24  ;      int     r1     =     30       r2     =     10  ;      circle  (  x1       y1       x2       y2       r1       r2  );      return     0  ;   }   
Java
   // Java program to check if two   // circles touch each other or not.   import     java.io.*  ;   class   GFG     {      static     void     circle  (  int     x1       int     y1       int     x2       int     y2        int     r1       int     r2  )      {      double     d     =     Math  .  sqrt  ((  x1     -     x2  )     *     (  x1     -     x2  )      +     (  y1     -     y2  )     *     (  y1     -     y2  ));      if     (  d      <=     r1     -     r2  )     {      System  .  out  .  println  (  'Circle B is inside A'  );      }      else     if     (  d      <=     r2     -     r1  )     {      System  .  out  .  println  (  'Circle A is inside B'  );      }      else     if     (  d      <     r1     +     r2  )     {      System  .  out  .  println  (  'Circle intersect'      +     ' to each other'  );      }      else     if     (  d     ==     r1     +     r2  )     {      System  .  out  .  println  (  'Circle touch to'      +     ' each other'  );      }      else     {      System  .  out  .  println  (  'Circle not touch'      +     ' to each other'  );      }      }      // Driver code      public     static     void     main  (  String  []     args  )      {      int     x1     =     -  10       y1     =     8  ;      int     x2     =     14       y2     =     -  24  ;      int     r1     =     30       r2     =     10  ;      circle  (  x1       y1       x2       y2       r1       r2  );      }   }   // This article is contributed by vt_m.   
Python
   # Python program to check if two   # circles touch each other or not.   import   math   # Function to check if two circles touch each other   def   circle  (  x1     y1     x2     y2     r1     r2  ):   d   =   math  .  sqrt  ((  x1   -   x2  )   *   (  x1   -   x2  )   +   (  y1   -   y2  )   *   (  y1   -   y2  ))   if  (  d    <=   r1   -   r2  ):   print  (  'Circle B is inside A'  )   elif  (  d    <=   r2   -   r1  ):   print  (  'Circle A is inside B'  )   elif  (  d    <   r1   +   r2  ):   print  (  'Circle intersect to each other'  )   elif  (  d   ==   r1   +   r2  ):   print  (  'Circle touch to each other'  )   else  :   print  (  'Circle not touch to each other'  )   # Driver code   x1     y1   =   -  10     8   x2     y2   =   14     -  24   r1     r2   =   30     10   # Function call   circle  (  x1     y1     x2     y2     r1     r2  )   # This code is contributed by Aman Kumar   
C#
   // C# program to check if two   // circles touch each other or not.   using     System  ;   class     GFG     {      static     void     circle  (  int     x1       int     y1       int     x2       int     y2        int     r1       int     r2  )      {      double     d     =     Math  .  Sqrt  ((  x1     -     x2  )     *     (  x1     -     x2  )      +     (  y1     -     y2  )     *     (  y1     -     y2  ));      if     (  d      <=     r1     -     r2  )     {      Console  .  Write  (  'Circle B is inside A'  );      }      else     if     (  d      <=     r2     -     r1  )     {      Console  .  Write  (  'Circle A is inside B'  );      }      else     if     (  d      <     r1     +     r2  )     {      Console  .  Write  (  'Circle intersect'      +     ' to each other'  );      }      else     if     (  d     ==     r1     +     r2  )     {      Console  .  Write  (  'Circle touch to'      +     ' each other'  );      }      else     {      Console  .  Write  (  'Circle not touch'      +     ' to each other'  );      }      }      // Driver code      public     static     void     Main  (  String  []     args  )      {      int     x1     =     -  10       y1     =     8  ;      int     x2     =     14       y2     =     -  24  ;      int     r1     =     30       r2     =     10  ;      circle  (  x1       y1       x2       y2       r1       r2  );      }   }   // This article is contributed by Pushpesh Raj.   
JavaScript
   // JavaScript program to check if two circles touch each other or not.   function     circle  (  x1       y1       x2       y2       r1       r2  )     {      var     d     =     Math  .  sqrt  ((  x1     -     x2  )     *     (  x1     -     x2  )     +     (  y1     -     y2  )     *     (  y1     -     y2  ));      if     (  d      <=     r1     -     r2  )     {      console  .  log  (  'Circle B is inside A'  );      }     else     if     (  d      <=     r2     -     r1  )     {      console  .  log  (  'Circle A is inside B'  );      }     else     if     (  d      <     r1     +     r2  )     {      console  .  log  (  'Circle intersect to each other'  );      }     else     if     (  d     ===     r1     +     r2  )     {      console  .  log  (  'Circle touch to each other'  );      }     else     {      console  .  log  (  'Circle not touch to each other'  );      }   }   // Driver code   var     x1     =     -  10       y1     =     8  ;   var     x2     =     14       y2     =     -  24  ;   var     r1     =     30       r2     =     10  ;   circle  (  x1       y1       x2       y2       r1       r2  );   // this code is contributed by devendra   

תְפוּקָה
Circle touch to each other 

מורכבות זמן: O(log(n)) מכיוון שמשתמשים בפונקציית sqrt מובנית 
מרחב עזר: O(1)


מאמר זה נתרם על ידי ארטי_ראתי ו דהרמנדרה קומאר .

צור חידון