Проверьте, касаются ли два заданных круга друг друга или пересекаются

Опубликовано: 19 Января, 2022

Есть две окружности 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
Ввод: -10 8
        14-24 
        30 10
Вывод: Круги касаются друг друга.

Рекомендуется: сначала решите эту проблему на «ПРАКТИКЕ», прежде чем переходить к решению.

Расстояние между центрами C1 и C2 рассчитывается как
C1C2 = sqrt ((x1 - x2) 2 + (y1 - y2) 2).
Возникает три условия.
1. Если C1C2 == R1 + R2
     Круг A и B касаются друг друга.
2. Если C1C2> R1 + R2
     Круг A и B не соприкасаются друг с другом.
3. Если C1C2 <R1 + R2
      Круг пересекает друг друга.

 

C++

// C++ program to check if two
// circles touch each other or not.
#include <bits/stdc++.h>
using namespace std;
 
int circle(int x1, int y1, int x2,
           int y2, int r1, int r2)
{
    int distSq = (x1 - x2) * (x1 - x2) +
                 (y1 - y2) * (y1 - y2);
    int radSumSq = (r1 + r2) * (r1 + r2);
    if (distSq == radSumSq)
        return 1;
    else if (distSq > radSumSq)
        return -1;
    else
        return 0;
}
 
// Driver code
int main()
{
    int x1 = -10, y1 = 8;
    int x2 = 14, y2 = -24;
    int r1 = 30, r2 = 10;
    int t = circle(x1, y1, x2,
                   y2, r1, r2);
    if (t == 1)
        cout << "Circle touch to"
             << " each other.";
    else if (t < 0)
        cout << "Circle not touch"
             << " to each other.";
    else
        cout << "Circle intersect"
             << " to each other.";
    return 0;
}

Java

// Java program to check if two
// circles touch each other or not.
import java.io.*;
 
class GFG
{
    static int circle(int x1, int y1, int x2,
                      int y2, int r1, int r2)
    {
        int distSq = (x1 - x2) * (x1 - x2) +
                     (y1 - y2) * (y1 - y2);
        int radSumSq = (r1 + r2) * (r1 + r2);
        if (distSq == radSumSq)
            return 1;
        else if (distSq > radSumSq)
            return -1;
        else
            return 0;
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int x1 = -10, y1 = 8;
        int x2 = 14, y2 = -24;
        int r1 = 30, r2 = 10;
        int t = circle(x1, y1, x2,
                       y2, r1, r2);
        if (t == 1)
            System.out.println ( "Circle touch to" +
                                 " each other.");
        else if (t < 0)
            System.out.println ( "Circle not touch" +
                                 " to each other.");
        else
            System.out.println ( "Circle intersect" +
                                 " to each other.");
             
    }
}
 
// This article is contributed by vt_m.

Python3

# Python3 program to
# check if two circles touch
# each other or not.
 
def circle(x1, y1, x2, y2, r1, r2):
  
    distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
    radSumSq = (r1 + r2) * (r1 + r2);
    if (distSq == radSumSq):
        return 1
    elif (distSq > radSumSq):
        return -1
    else:
        return 0
  
 
# Driver code
x1 = -10
y1 = 8
x2 = 14
y2 = -24
r1 = 30
r2 = 10
 
t = circle(x1, y1, x2, y2, r1, r2)
if (t == 1):
    print("Circle touch to each other.")
elif (t < 0):
    print("Circle not touch to each other.")
else:
    print("Circle intersect to each other.")
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to check if two
// circles touch each other or not.
using System;
 
class GFG
{
    static int circle(int x1, int y1, int x2,
                      int y2, int r1, int r2)
    {
        int distSq = (x1 - x2) * (x1 - x2) +
                     (y1 - y2) * (y1 - y2);
        int radSumSq = (r1 + r2) * (r1 + r2);
        if (distSq == radSumSq)
            return 1;
        else if (distSq > radSumSq)
            return -1;
        else
            return 0;
    }
 
    // Driver code
    public static void Main ()
    {
        int x1 = -10, y1 = 8;
        int x2 = 14, y2 = -24;
        int r1 = 30, r2 = 10;
        int t = circle(x1, y1, x2,
                       y2, r1, r2);
        if (t == 1)
            Console.WriteLine ( "Circle touch" +
                             " to each other.");
        else if (t < 0)
            Console.WriteLine( "Circle not touch" +
                                " to each other.");
        else
            Console.WriteLine ( "Circle intersect" +
                                 " to each other.");
             
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to check if two
// circles touch each other or not.
 
function circle($x1, $y1, $x2,
                $y2, $r1, $r2)
{
    $distSq = ($x1 - $x2) * ($x1 - $x2) +
              ($y1 - $y2) * ($y1 - $y2);
    $radSumSq = ($r1 + $r2) * ($r1 + $r2);
    if ($distSq == $radSumSq)
        return 1;
    else if ($distSq > $radSumSq)
        return -1;
    else
        return 0;
}
 
// Driver code
$x1 = -10; $y1 = 8;
$x2 = 14; $y2 = -24;
$r1 = 30; $r2 = 10;
$t = circle($x1, $y1, $x2,
            $y2, $r1, $r2);
if ($t == 1)
    echo "Circle touch to each other.";
else if ($t < 0)
    echo "Circle not touch to each other.";
else
    echo "Circle intersect to each other.";
 
// This code is contributed by vt_m.
?>

Javascript

<script>
 
// JavaScript program to check if two
// circles touch each other or not.
 
function circle(x1, y1, x2,
                      y2, r1, r2)
    {
        let distSq = (x1 - x2) * (x1 - x2) +
                     (y1 - y2) * (y1 - y2);
        let radSumSq = (r1 + r2) * (r1 + r2);
        if (distSq == radSumSq)
            return 1;
        else if (distSq > radSumSq)
            return -1;
        else
            return 0;
    }
 
// Driver Code
        let x1 = -10, y1 = 8;
        let x2 = 14, y2 = -24;
        let r1 = 30, r2 = 10;
        let t = circle(x1, y1, x2,
                       y2, r1, r2);
        if (t == 1)
            document.write ( "Circle touch to" +
                                 " each other.");
        else if (t < 0)
            document.write( "Circle not touch" +
                                 " to each other.");
        else
            document.write ( "Circle letersect" +
                                 " to each other.");
  
 // This code is contributed by susmitakundugoaldanga.
</script>

Выход :

Circle touch to each other.

Эта статья предоставлена Дхармендрой Кумаром . Если вам нравится GeeksforGeeks, и вы хотели бы внести свой вклад, вы также можете написать статью, используя write.geeksforgeeks.org, или отправить свою статью по электронной почте: deposit@geeksforgeeks.org. Посмотрите, как ваша статья появляется на главной странице GeeksforGeeks, и помогите другим гикам.
Пожалуйста, напишите комментарии, если вы обнаружите что-то неправильное, или вы хотите поделиться дополнительной информацией по теме, обсужденной выше.

Вниманию читателя! Не прекращайте учиться сейчас. Освойте все важные концепции DSA с помощью самостоятельного курса DSA по приемлемой для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .

Если вы хотите посещать живые занятия с отраслевыми экспертами, пожалуйста, обращайтесь к Geeks Classes Live и Geeks Classes Live USA.