Length of intercept cut off from a line by a Circle | Function to find the radius of a circle ; g and f are the coordinates of the center ; Case of invalid circle ; Apply the radius formula ; Function to find the perpendicular distance between circle center and the line ; Store the coordinates of center ; Stores the perpendicular distance between the line and the point ; Invalid Case ; Return the distance ; Function to find the length of intercept cut off from a line by a circle ; Calculate the value of radius ; Calculate the perpendicular distance between line and center ; Invalid Case ; If line do not cut circle ; Print the intercept length ; Given Input ; Function Call
function radius ( a , b , c ) { let g = a / 2 ; let f = b / 2 ; if ( g * g + f * f - c < 0 ) return ( - 1 ) ; return ( Math . sqrt ( g * g + f * f - c ) ) ; } function centerDistanceFromLine ( a , b , i , j , k ) { let g = a / 2 ; let f = b / 2 ; let distance = Math . abs ( i * g + j * f + k ) / ( Math . sqrt ( i * i + j * j ) ) ; if ( distance < 0 ) return ( - 1 ) ; return distance ; } function interceptLength ( a , b , c , i , j , k ) { let rad = radius ( a , b , c ) ; let dist = centerDistanceFromLine ( a , b , i , j , k ) ; if ( rad < 0 dist < 0 ) { document . write ( " " ) ; return ; } if ( dist > rad ) { document . write ( " " ) ; } else document . write ( 2 * Math . sqrt ( rad * rad - dist * dist ) ) ; } let a = 0 , b = 0 , c = - 4 ; let i = 2 , j = - 1 , k = 1 ; interceptLength ( a , b , c , i , j , k ) ;