Radius of the inscribed circle within three tangent circles | Radius of the 3 given circles declared as double . ; 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
let r1 , r2 , r3 ; function area ( a , b , c ) { let p = ( a + b + c ) / 2 ; return Math . sqrt ( p ) * Math . sqrt ( p - a ) * Math . sqrt ( p - b ) * Math . sqrt ( p - c ) ; } function binary_search ( ) { let s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) ; let l = 0 , h = s / ( r1 + r2 + r3 ) ; while ( h - l >= 1.e-7 ) { let mid = ( l + h ) / 2 ; let s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) ; let s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) ; let s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) ; if ( s1 + s2 + s3 < s ) { l = mid ; } else { h = mid ; } } return ( l + h ) / 2 ; } r1 = 1.0 ; r2 = 2.0 ; r3 = 3.0 ; document . write ( binary_search ( ) . toPrecision ( 6 ) ) ;