Given a time, calculate the angle between the hour and minute hands

1.9k Views Asked by At

I cannot understand the solution to the following programming problem. I will be very thankful for you help!

Given a time, calculate the angle between the hour and minute hands

Solution:

• Angle between the minute hand and 12 o'clock: 360 * m / 60

• Angle between the hour hand and 12 o'clock:

360 * (h % 12) / 12 + 360 * (m / 60) * (1 / 12)

• Angle between hour and minute:

(hour angle - minute angle) % 360

This reduces to

(30h - 5.5m)%360

2

There are 2 best solutions below

4
On BEST ANSWER

After $x$ hours of time, the hour hand travels $x / 12$ rotations around the clock. So after $x$ minutes, it travels $x / (60 \cdot 12) = x / 720$.

After $x$ minutes of time, the minute hand travels $x / 60$ rotations around the clock.

At 12:00, both the hour and the minute hand are at position $0$.

Given a time hh:mm, first figure out how many minutes it has been since 12:00. This will be $60$ times the number of hours hh, plus the number of minutes mm. Set this value as $x$. Then you get that the minute hand has traveled $x / 60$ rotations, and the hour hand has traveled $x / 720$ rotations. Subtracting the two, the angle between them is $(x / 60) - (x / 720) = 11 x / 720$ rotations. Next, convert this number of rotations to degrees by multiplying by $360$; you get $11x / 2$ degrees. However, you need to reduce this mod $360$, so that the angle you get is between $180$ and $-180$. Finally, if it's negative, return the absolute value of the result.

0
On

Angle between the minute hand and 12 o'clock:

$m/60$ is the percentage of the clock circle. E.g if we have $15$ minutes on the clock then $15/60$ is the percentage of the circle that the minute hand passed. $360 * m/60$ simply means what percentage of 360 degrees (e.g actual degree) minute hand passed.

Angle between the hour hand and 12 o'clock:

Same logic applies here: in $(h \mod 12)$ - we handle military time

$(h \mod 12)/12$ is the percentage of the clock circle $360 * (h \mod 12)/12$ is percentage of 360 degrees (e.g actual degree) hour hand passed.

Angle between hour and minute hand: Well, here is simple: You have one degree and another degree e.g $0$ degree hour hand and $270$ degree minute hand then you take absolute value of their difference e.g $-270 = 270$

angle = abs(hours_degree - minutes_degree)

and then you take Math.min(angle, 360 - angle) because maximum angle is 180. I am not sure what programming language you are using, but you got the point.