Open In App
Related Articles

Descartes’ Circle Theorem with implementation

Improve Article
Improve
Save Article
Save
Like Article
Like

In geometry, Descartes’ theorem states that for every four mutually tangent circles, the radii of the circles satisfy a certain quadratic equation. One can construct a fourth circle tangent to three given, mutually tangent circles. 
Descartes’ Circle Theorem helps us to find the radius of a circle when there are 4 circles with positive integer radius r1, r2, r3 and r4 as shown in the figure below. It finds the radius r4 of the circle formed by three circles of radius r1, r2, r3 as shown in the image below. 
(Note that the circles in the picture below are tangent to each other.)

Examples: 

Input: r1 = 1, r2 = 1, r3 = 1 
Output: 0.154701

Input: r1 =23, r2 = 46, r3 = 69 
Output: 6.000000

 

The theorem says that the reciprocals of radii, or “curvatures”, of these circles 

k_1 = 1/r_1, k_2 = 1/r_2, k_3 = 1/r_3\:and\:k_4 = 1/r_4
satisfies the following relation:
2(k_1^2+k_2^2+k_3^2+k_4^2) = (k_1+k_2+k_3+k_4)^2

One of the circles can be enclosed among all the remaining three, as in the figure, in which case the corresponding curvature (k_4 = -1/r_4)            here is considered negative and the above relation still holds.
If k_1, k_2, k_3            are known, one can solve for k4, 
k_4 = k_1 + k_2 + k_3 \pm 2\sqrt{k_1 k_2 + k_2 k_3 + k_1 k_3}

On solving the above equation we get the radius of the fourth circle. The formula for finding the fourth circle’s radius is: 

r_4 = (r_1*r_2*r_3)\, /\, (r_1*r_2 + r_2*r_3 + r_3*r_1 + 2.0\, *\, \sqrt{(r_1*r_2*r_3*(r_1+r_2+r_3))})

Thus if r1, r2, and r3 are known, r4 can easily be calculated using the above formula. 

Below is the implementation of the above approach: 

CPP




// C++ implementation of the
// above formulae
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the fourth circle's
// when three radius are given
double findRadius(double r1, double r2, double r3)
{
    double r4 = (r1 * r2 * r3)
                / (r1 * r2 + r2 * r3
                   + r1 * r3 + 2.0 * sqrt(r1 * r2 * r3 * (r1 + r2 + r3)));
 
    return r4;
}
 
// Driver code
int main()
{
    // Radius of three circles
    double r1 = 1;
    double r2 = 1;
    double r3 = 1;
 
    // Calculation of r4 using formula given above
    double r4 = findRadius(r1, r2, r3);
 
    cout << "The radius of fourth circle: " << r4;
    return 0;
}


Java




/*package whatever //do not write package name here */
// Java implementation of the
// above formulae
import java.io.*;
 
class GFG
{
   
        // Function to find the fourth circle's
        // when three radius are given
        static double findRadius(double r1, double r2, double r3)
        {
        double r4 = (r1 * r2 * r3)
                / (r1 * r2 + r2 * r3
                   + r1 * r3 + 2.0 * Math.sqrt(r1 * r2 * r3 * (r1 + r2 + r3)));
 
        return r4;
        }
   
      // Driver code
    public static void main (String[] args)
    {
       
        // Radius of three circles
    double r1 = 1;
    double r2 = 1;
    double r3 = 1;
 
    // Calculation of r4 using formula given above
    double r4 = findRadius(r1, r2, r3);
        System.out.println("The radius of fourth circle: " + r4);
    }
}
 
// This code is contributed by CoderSaty.


Python3




# Python 3 implementation of the
# above formulae
 
from math import sqrt
 
# Function to find the fourth circle's
# when three radius are given
def findRadius(r1, r2, r3):
    r4 = (r1 * r2 * r3) / (r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * sqrt(r1 * r2 * r3 * (r1 + r2 + r3)))
 
    return r4
 
# Driver code
if __name__ == '__main__':
   
    # Radius of three circles
    r1 = 1
    r2 = 1
    r3 = 1
 
    # Calculation of r4 using formula given above
    r4 = findRadius(r1, r2, r3)
 
    print("The radius of fourth circle:",r4)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#




// C# implementation of the
// above formulae
using System;
 
class GFG
{
 
  // Function to find the fourth circle's
  // when three radius are given
  static double findRadius(double r1, double r2, double r3)
  {
    double r4 = (r1 * r2 * r3)
      / (r1 * r2 + r2 * r3
         + r1 * r3 + 2.0 * Math.Sqrt(r1 * r2 * r3 * (r1 + r2 + r3)));
 
    return r4;
  }
 
  // Driver code
  public static void Main (String[] args)
  {
 
    // Radius of three circles
    double r1 = 1;
    double r2 = 1;
    double r3 = 1;
 
    // Calculation of r4 using formula given above
    double r4 = Math.Round(findRadius(r1, r2, r3),6);
 
    Console.Write("The radius of fourth circle: " + r4);
  }
}
 
// This code is contributed by shivanisinghss2110


Javascript




<script>
// Javascript implementation of the
// above formulae
 
// Function to find the fourth circle's
// when three radius are given
function findRadius(r1, r2, r3) {
  let r4 =
    (r1 * r2 * r3) /
    (r1 * r2 +
      r2 * r3 +
      r1 * r3 +
      2.0 * Math.sqrt(r1 * r2 * r3 * (r1 + r2 + r3)));
 
  return r4.toFixed(6);
}
 
// Driver code
 
// Radius of three circles
let r1 = 1;
let r2 = 1;
let r3 = 1;
 
// Calculation of r4 using formula given above
let r4 = findRadius(r1, r2, r3);
 
document.write("The radius of fourth circle: " + r4);
 
// This code is contributed by gfgking.
</script>


Output

The radius of fourth circle: 0.154701

Time complexity: O(log(r1 * r2 * r3 * (r1 + r2 + r3)))
Auxiliary space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 23 Nov, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials