Length of intercept cut off from a line by a Circle | Python3 program for the above approach ; 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 ; Driver Code ; Given Input ; Function Call
import math NEW_LINE def radius ( a , b , c ) : NEW_LINE INDENT g = a / 2 NEW_LINE f = b / 2 NEW_LINE if ( g * g + f * f - c < 0 ) : NEW_LINE INDENT return ( - 1 ) NEW_LINE DEDENT return ( math . sqrt ( g * g + f * f - c ) ) NEW_LINE DEDENT def centerDistanceFromLine ( a , b , i , j , k ) : NEW_LINE INDENT g = a / 2 NEW_LINE f = b / 2 NEW_LINE distance = ( abs ( i * g + j * f + k ) / ( math . sqrt ( i * i + j * j ) ) ) NEW_LINE if ( distance < 0 ) : NEW_LINE INDENT return ( - 1 ) NEW_LINE DEDENT return distance NEW_LINE DEDENT def interceptLength ( a , b , c , i , j , k ) : NEW_LINE INDENT rad = radius ( a , b , c ) NEW_LINE dist = centerDistanceFromLine ( a , b , i , j , k ) NEW_LINE if ( rad < 0 or dist < 0 ) : NEW_LINE INDENT print ( " circle ▁ not ▁ possible " ) NEW_LINE return NEW_LINE DEDENT if ( dist > rad ) : NEW_LINE INDENT print ( " Line ▁ not ▁ cutting ▁ circle " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 2 * math . sqrt ( rad * rad - dist * dist ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 0 NEW_LINE b = 0 NEW_LINE c = - 4 NEW_LINE i = 2 NEW_LINE j = - 1 NEW_LINE k = 1 NEW_LINE interceptLength ( a , b , c , i , j , k ) NEW_LINE DEDENT