How do I calculate the angle between the hour and minutes hands?

Viewed 41613

I'm trying to workout this problem, but I am still struggling to understand the logic to solve this problem.

hour degree = 360 / 12 = 30
minutes degree = 360 / 12 / 60 = 0.5

So, according to this, I thought I could formulate the following function in python:

def clockangles(hour, min):
    return (hour * 30) + (min * 0.5)

For the hour, it works fine, as it appears to have a 1=1 mapping. But for the minute there is one problem at least. When it's 0 minutes, the minutes hand points to 12.

For example:

7pm: hands pointing to 7pm and minutes pointing to 12

How do I calculate the minutes properly? Please help me understand the formula.

EDIT: For example, if I call the function above with 7pm, e.g clockangles(7,0) I get the value 210. However, according this link the angle at 7:00 is 150

11 Answers

returns minimum non negative angle, supports 24 time format:

def clockangles(hour, minute):
    return -abs(abs((hour%12)*30 - minute*6)-180)+180

This version of the accepted answer simplifies the formula in the second line. It returns the same result for all values of hour and minute.

def get_angles(hour, minute):
    ans = abs(11 * minute - 60 * hour) / 2
    return min(360 - ans, ans)
Related