14 CLOCK ANGLE
Let's take 12:00 (h = 12, m = 0) as a reference and do below steps.
1. Calculate the angle made by hour hand with respect to 12:00 in h hours and m minutes.
2. Calculate the angle made by minute hand with respect to 12:00 in h hours and m minutes.
3. The difference between the two angles is the angle between the two hands.
The hour hand moves 360 degrees in 12 hours (or 0.5 degrees in 1 minute) and minute hand moves 360 degrees in 60 minute (or 6 degrees in one minute).
In h hours and m minutes, the hour hand would move (60 (h % 12) + m )*0.5 and minute hand would move 6*m.
So required angle = (60 (h % 12) + m )*0.5 - 6*m
= 30 * (h % 12) + 0.5*m - 6*m
= 30 * (h % 12) + 5.5*m
def clock_angle(time):
hour, minutes = map(int, time.split(':'))
angle = abs(30 * (hour % 12) - 5.5 * minutes)
return min(angle, 360 - angle)
# T TIME
T='3:30'
print('Angle :', clock_angle(T), 'degree')
If T=’12:00’ then output:
Angle : 0.0 degree
If T=’3:30’ then output:
Angle : 75.0 degree