Distance between end points of Hour and minute hand at given time | Python3 implementation to find the distance between the end points of the hour and minute hand ; Function to find the angle between Hour hand and minute hand ; Validate the input ; Calculate the angles moved by hour and minute hands with reference to 12 : 00 ; Find the difference between two angles ; Return the smaller angle of two possible angles ; Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum along the series ; Holds the actual value of sin ( n ) ; Function to distance between the endpoints of the hour and minute hand ; Time ; Length of hour hand ; Length of minute hand ; calling Function for finding angle between hour hand and minute hand ; Function for finding distance between end points of minute hand and hour hand
import math NEW_LINE def calcAngle ( h , m ) : NEW_LINE INDENT if ( h < 0 or m < 0 or h > 12 or m > 60 ) : NEW_LINE INDENT print ( " Wrong ▁ input " ) NEW_LINE DEDENT if ( h == 12 ) : NEW_LINE INDENT h = 0 NEW_LINE DEDENT if ( m == 60 ) : NEW_LINE INDENT m = 0 NEW_LINE DEDENT hour_angle = 0.5 * ( h * 60 + m ) NEW_LINE minute_angle = 6 * m NEW_LINE angle = abs ( hour_angle - minute_angle ) NEW_LINE angle = min ( 360 - angle , angle ) NEW_LINE return angle NEW_LINE DEDENT def cal_cos ( n ) : NEW_LINE INDENT accuracy = 0.0001 NEW_LINE n = n * ( 3.142 / 180.0 ) NEW_LINE x1 = 1 NEW_LINE cosx = x1 NEW_LINE cosval = math . cos ( n ) NEW_LINE i = 1 NEW_LINE while True : NEW_LINE INDENT denominator = 2 * i * ( 2 * i - 1 ) NEW_LINE x1 = - x1 * n * n / denominator NEW_LINE cosx = cosx + x1 NEW_LINE i = i + 1 NEW_LINE if accuracy > math . fabs ( cosval - cosx ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return cosx NEW_LINE DEDENT def distanceEndpoints ( a , b , c ) : NEW_LINE INDENT angle = cal_cos ( c ) NEW_LINE return math . sqrt ( ( a * a ) + ( b * b ) - 2 * a * b * angle ) NEW_LINE DEDENT hour = 3 NEW_LINE Min = 30 NEW_LINE hourHand = 3 NEW_LINE minHand = 4 NEW_LINE angle = calcAngle ( hour , Min ) NEW_LINE distance = distanceEndpoints ( minHand , hourHand , angle ) NEW_LINE print ( ' % .5f ' % distance ) NEW_LINE