# Check if two given circles touch or intersect each other

• Difficulty Level : Easy
• Last Updated : 03 Aug, 2022

There are two circles A and B with their centres C1(x1, y1) and C2(x2, y2) and radius R1 and R2. The task is to check both circles A and B touch each other or not.

Examples :

Input : C1 = (3,  4)
C2 = (14, 18)
R1 = 5, R2 = 8
Output : Circles do not touch each other.

Input : C1 = (2,  3)
C2 = (15, 28)
R1 = 12, R2 = 10
Output : Circles intersect with each other.

Input : C1 = (-10,  8)
C2 = (14, -24)
R1 = 30, R2 = 10

Approach:
Distance between centres C1 and C2 is calculated as

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

There are three conditions that arise.

1. If C1C2 <= R1 – R2: Circle B is inside A.
2. If C1C2 <= R2 – R1: Circle A is inside B.
3. If C1C2 < R1 + R2: Circle intersects each other.
4. If C1C2 == R1 + R2: Circle A and B are in touch with each other.
5. Otherwise, Circle A and  do not overlap

Below is the implementation of the above approach:

## 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.`

Output

`Circle touch to each other`

Time Complexity: O(log(n)) because using inbuilt sqrt function
Auxiliary Space: O(1)

This article is contributed by Aarti_Rathi and Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.