Radius of the inscribed circle within three tangent circles | Python3 implementation of the approach ; Radius of the 3 given circles ; Calculation of area of a triangle by Heron 's formula ; Applying binary search to find the radius r4 of the required circle ; Area of main triangle ; Loop runs until l and h becomes approximately equal ; Area of smaller triangles ; If sum of smaller triangles is less than main triangle ; If sum of smaller triangles is greater than or equal to main triangle ; Taking r1 , r2 , r3 as input ; Call to function binary search
import math NEW_LINE r1 = 0 NEW_LINE r2 = 0 NEW_LINE r3 = 0 NEW_LINE def area ( a , b , c ) : NEW_LINE INDENT p = ( a + b + c ) / 2 NEW_LINE return ( ( math . sqrt ( p ) ) * ( math . sqrt ( p - a ) ) * ( math . sqrt ( p - b ) ) * ( math . sqrt ( p - c ) ) ) NEW_LINE DEDENT def binary_search ( ) : NEW_LINE INDENT global r1 , r2 , r3 NEW_LINE s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) NEW_LINE l = 0 NEW_LINE h = s / ( r1 + r2 + r3 ) NEW_LINE while ( h - l > 0.00000001 ) : NEW_LINE INDENT mid = ( l + h ) / 2 NEW_LINE s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) NEW_LINE s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) NEW_LINE s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) NEW_LINE if ( s1 + s2 + s3 < s ) : NEW_LINE INDENT l = mid NEW_LINE DEDENT else : NEW_LINE INDENT h = mid NEW_LINE DEDENT DEDENT return ( ( l + h ) / 2 ) NEW_LINE DEDENT r1 = 1 NEW_LINE r2 = 2 NEW_LINE r3 = 3 NEW_LINE print ( " { 0 : . 6f } " . format ( binary_search ( ) ) ) NEW_LINE