# Check if two given circles touch or intersect each other

There are two circle A and B with their centers **C1(x1, y1)** and **C2(x2, y2)** and radius **R1** and **R2**. 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 Input : -10 8 14 -24 30 10 Output : Circle touch each other.

Distance between centers C1 and C2 is calculated asThere are three condition arises. 1. IfC1C2 = sqrt((x1 - x2)^{2}+ (y1 - y2)^{2}).Circle A and B are touch to each other. 2. IfC1C2 == R1 + R2Circle A and B are not touch to each other. 3. IfC1C2 > R1 + R2Circle intersects each other.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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output :**

Circle touch to each other.

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- How to check if two given line segments intersect?
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Check whether given circle resides in boundary maintained by two other circles
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Path in a Rectangle with Circles
- Maximum points of intersection n circles
- Program to calculate the area between two Concentric Circles
- Length of direct common tangent between two intersecting Circles
- Length of direct common tangent between the two non-intersecting Circles
- Length of the transverse common tangent between the two non intersecting circles
- Number of common tangents between two circles if their centers and radius is given
- Length of rope tied around three equal circles touching each other
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given