Skip to content
Related Articles
Check if two circles intersect such that the third circle passes through their points of intersections and centers
• Last Updated : 21 May, 2021

Given centres and the radii of three circles A, B, and C in the form of {X, Y, R}, where (X, Y) is the centre of the circle and R is the radius of that circle. The task is to check if any two circles intersect such that the third circle passes through the intersecting points and the centres of the two circles. If found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

Input: A = {0, 0, 8}, B = {0, 10, 6}, C = {0, 5, 5}
Output: Yes

Input: arr[] = {{5, 0, 5}, {15, 0, 2}, {20, 0, 1}}
Output: No

Approach: The given problem can be solved based on the following observations: • Suppose C1C2, C1C3, and C2C3 are the distance between the centre of the circle C1 and C2, C1 and C3, and C2 and C3 respectively.
• Then the circles C1 and C2 intersect if C1C2 < r1 + r2.
• Now, since the third circle should pass through the centre of the two circles and their intersecting points, the line through C1 and C2 becomes perpendicular bisector for the chord S1S2 as shown in the figure.
• So it can be observed that now C1C3 is equal to C3C2 it and C1C3 is the radius of the third circle and the centre of the third circle is the midpoint of C1 and C2.
• Therefore, the given three circles satisfy the given criteria if and only if the centre of C3 becomes the midpoint of C1 and C2 and if C1 and C2 intersect and the radius of C3 becomes half of C1C2.

Follow the steps below to solve the problem:

• Generate every combination of the given three circles and perform the following steps:
• Find the distance between the centre of the first two circles and store it in a variable say C1C2.
• If C1C2 is less than the sum of radii of the first two circles and the centre of the third circle is the midpoint of the centres of the first two circles, then print “Yes”.
• After completing the above steps, if there doesn’t exist any combination of circles satisfying the given criteria, then print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Structure of the circle``class` `circle {``public``:``    ``double` `x;``    ``double` `y;``    ``double` `r;``};` `//Utility function to check if given``// circles satisfy required criteria``bool` `check(circle C[])``{``    ``// Stores the distance between``    ``// the centres of C1 and C2``    ``double` `C1C2``        ``= ``sqrt``((C.x - C.x)``                   ``* (C.x - C.x)``               ``+ (C.y - C.y)``                     ``* (C.y - C.y));` `    ``// Stores the status if the given``    ``// given criteria is satisfied or not``    ``bool` `flag = 0;` `    ``// If C1C2 is less than the sum of``    ``// the radii of the first 2 circles``    ``if` `(C1C2 < (C.r + C.r)) {` `        ``// If C3 is the midpoint of``        ``// the centres at C1 and C2``        ``if` `((C.x + C.x)``                ``== 2 * C.x``            ``&& (C.y + C.y)``                   ``== 2 * C.y) {` `            ``// Mark flag true``            ``flag = 1;``        ``}``    ``}` `    ``// Return flag``    ``return` `flag;``}` `// Function to check if the given``// circles satisfy required criteria``bool` `IsFairTriplet(circle c[])``{``    ``bool` `f = ``false``;` `    ``// Check for the current``    ``// combination of circles``    ``f |= check(c);` `    ``for` `(``int` `i = 0; i < 2; i++) {` `        ``swap(c, c);` `        ``// Check for the next``        ``// combination``        ``f |= check(c);``    ``}` `    ``return` `f;``}` `// Driver Code``int` `main()``{``    ``circle C;``    ``C = { 0, 0, 8 };``    ``C = { 0, 10, 6 };``    ``C = { 0, 5, 5 };` `    ``if` `(IsFairTriplet(C))``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;` `    ``return` `0;``}`

## Java

 `// Java approach for the above approach``class` `GFG{` `// Structure of the circle``static` `class` `circle``{``    ``double` `x;``    ``double` `y;``    ``double` `r;` `    ``public` `circle(``int` `x, ``int` `y, ``int` `r)``    ``{``        ``this``.x = x;``        ``this``.y = y;``        ``this``.r = r;``    ``}``}` `// Utility function to check if given``// circles satisfy required criteria``static` `boolean` `check(circle C[])``{``    ` `    ``// Stores the distance between``    ``// the centres of C1 and C2``    ``double` `C1C2 = Math.sqrt(``        ``(C[``1``].x - C[``0``].x) * (C[``1``].x - C[``0``].x) +``        ``(C[``1``].y - C[``0``].y) * (C[``1``].y - C[``0``].y));` `    ``// Stores the status if the given``    ``// given criteria is satisfied or not``    ``boolean` `flag = ``false``;` `    ``// If C1C2 is less than the sum of``    ``// the radii of the first 2 circles``    ``if` `(C1C2 < (C[``0``].r + C[``1``].r))``    ``{``        ` `        ``// If C3 is the midpoint of``        ``// the centres at C1 and C2``        ``if` `((C[``0``].x + C[``1``].x) == ``2` `* C[``2``].x &&``            ``(C[``0``].y + C[``1``].y) == ``2` `* C[``2``].y)``        ``{``            ` `            ``// Mark flag true``            ``flag = ``true``;``        ``}``    ``}` `    ``// Return flag``    ``return` `flag;``}` `// Function to check if the given``// circles satisfy required criteria``static` `boolean` `IsFairTriplet(circle c[])``{``    ``boolean` `f = ``false``;` `    ``// Check for the current``    ``// combination of circles``    ``f |= check(c);` `    ``for``(``int` `i = ``0``; i < ``2``; i++)``    ``{``        ``swap(c[``0``], c[``2``]);` `        ``// Check for the next``        ``// combination``        ``f |= check(c);``    ``}``    ``return` `f;``}` `static` `void` `swap(circle circle1, circle circle2)``{``    ``circle temp = circle1;``    ``circle1 = circle2;``    ``circle2 = temp;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``circle C[] = ``new` `circle[``3``];``    ``C[``0``] = ``new` `circle(``0``, ``0``, ``8``);``    ``C[``1``] = ``new` `circle(``0``, ``10``, ``6``);``    ``C[``2``] = ``new` `circle(``0``, ``5``, ``5``);` `    ``if` `(IsFairTriplet(C))``        ``System.out.println(``"Yes"``);``    ``else``        ``System.out.println(``"No"``);``}``}` `// This code is contributed by abhinavjain194`

## Python3

 `# Python3 program for the above approach``from` `math ``import` `sqrt` `# Structure of the circle``class` `circle:``    ``def` `__init__(``self``, a, b, c):``        ``self``.x ``=` `a``        ``self``.y ``=` `b``        ``self``.r ``=` `c` `# Utility function to check if given``# circles satisfy required criteria``def` `check(C):``  ` `    ``# Stores the distance between``    ``# the centres of C1 and C2``    ``C1C2 ``=` `sqrt((C[``1``].x ``-` `C[``0``].x)``                   ``*` `(C[``1``].x ``-` `C[``0``].x)``               ``+` `(C[``1``].y ``-` `C[``0``].y)``                     ``*` `(C[``1``].y ``-` `C[``0``].y))` `    ``# Stores the status if the given``    ``# given criteria is satisfied or not``    ``flag ``=` `0` `    ``# If C1C2 is less than the sum of``    ``# the radii of the first 2 circles``    ``if` `(C1C2 < (C[``0``].r ``+` `C[``1``].r)):` `        ``# If C3 is the midpoint of``        ``# the centres at C1 and C2``        ``if` `((C[``0``].x ``+` `C[``1``].x) ``=``=` `2` `*` `C[``2``].x ``and` `(C[``0``].y ``+` `C[``1``].y) ``=``=` `2` `*` `C[``2``].y):``            ``# Mark flag true``            ``flag ``=` `1` `    ``# Return flag``    ``return` `flag` `# Function to check if the given``# circles satisfy required criteria``def` `IsFairTriplet(c):``    ``f ``=` `False` `    ``# Check for the current``    ``# combination of circles``    ``f |``=` `check(c)` `    ``for` `i ``in` `range``(``2``):` `        ``c[``0``], c[``2``] ``=` `c[``2``], c[``0``]` `        ``# Check for the next``        ``# combination``        ``f |``=` `check(c)` `    ``return` `f` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``C ``=` `[circle(``0``,``0``,``0``) ``for` `i ``in` `range``(``3``)]``    ``C[``0``] ``=` `circle(``0``, ``0``, ``8``)``    ``C[``1``] ``=` `circle(``0``, ``10``, ``6``)``    ``C[``2``] ``=` `circle(``0``, ``5``, ``5``)` `    ``if` `(IsFairTriplet(C)):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# approach for the above approach``using` `System;` `class` `GFG{` `// Structure of the circle``class` `circle``{``    ``public` `double` `x;``    ``public` `double` `y;``    ``public` `double` `r;` `    ``public` `circle(``int` `x, ``int` `y, ``int` `r)``    ``{``        ``this``.x = x;``        ``this``.y = y;``        ``this``.r = r;``    ``}``}` `// Utility function to check if given``// circles satisfy required criteria``static` `bool` `check(circle []C)``{``    ` `    ``// Stores the distance between``    ``// the centres of C1 and C2``    ``double` `C1C2 = Math.Sqrt(``        ``(C.x - C.x) * (C.x - C.x) +``        ``(C.y - C.y) * (C.y - C.y));` `    ``// Stores the status if the given``    ``// given criteria is satisfied or not``    ``bool` `flag = ``false``;` `    ``// If C1C2 is less than the sum of``    ``// the radii of the first 2 circles``    ``if` `(C1C2 < (C.r + C.r))``    ``{``        ` `        ``// If C3 is the midpoint of``        ``// the centres at C1 and C2``        ``if` `((C.x + C.x) == 2 * C.x &&``            ``(C.y + C.y) == 2 * C.y)``        ``{``            ` `            ``// Mark flag true``            ``flag = ``true``;``        ``}``    ``}` `    ``// Return flag``    ``return` `flag;``}` `// Function to check if the given``// circles satisfy required criteria``static` `bool` `IsFairTriplet(circle []c)``{``    ``bool` `f = ``false``;` `    ``// Check for the current``    ``// combination of circles``    ``f |= check(c);` `    ``for``(``int` `i = 0; i < 2; i++)``    ``{``        ``swap(c, c);` `        ``// Check for the next``        ``// combination``        ``f |= check(c);``    ``}``    ``return` `f;``}` `static` `void` `swap(circle circle1, circle circle2)``{``    ``circle temp = circle1;``    ``circle1 = circle2;``    ``circle2 = temp;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``circle []C = ``new` `circle;``    ``C = ``new` `circle(0, 0, 8);``    ``C = ``new` `circle(0, 10, 6);``    ``C = ``new` `circle(0, 5, 5);` `    ``if` `(IsFairTriplet(C))``        ``Console.WriteLine(``"Yes"``);``    ``else``        ``Console.WriteLine(``"No"``);``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`Yes`

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up