1

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

  • Find the angle between each hand and a fixed reference point, i.e., 12 o'clock, then take the difference between the two angles to find the angle in between. What is it that you have difficulty with? – abiessu Jul 30 '15 at 17:59
  • In this context, what does "%" mean? – DJohnM Jul 30 '15 at 17:59
  • 1
    Can you edit this using MathJax to make it more legible? http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference could be helpful. Thanks – David Quinn Jul 30 '15 at 18:00
  • 2
    @DJohnM: it almost certainly means "modulo" – abiessu Jul 30 '15 at 18:08
  • @DavidQuinn I tried, but when I was putting $ $ around the second expression everything after "360 * (" was disappearing.. not sure why – ShockWaveMonster Jul 30 '15 at 21:13

2 Answers2

1

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.

  • I did not understand this part - " However, you need to reduce this mod 360". Could you provide an example or smth so I understand it? Thanks – ShockWaveMonster Jul 30 '15 at 22:17
  • @ShockWaveMonster For example, say the minute hand is $800$ degrees ahead of the hour hand. $360$ degrees is one circle, so the minute hand is two full circles, plus $80$ degrees ahead ($360 + 360 + 80 = 800$). Which is the same as just $80$ degrees ahead. – Caleb Stanford Jul 30 '15 at 22:29
  • how minute hand can be 800 degrees ahead? I mean 360 is max degree and two hands lie within 360 degrees. – ShockWaveMonster Jul 30 '15 at 23:30
  • @ShockWaveMonster Once you get up to 360 degrees ahead you keep going, 361, 362, and so on. But it's really the same as if you just went 359, 360, 1, 2, 3, starting over at 1. – Caleb Stanford Jul 31 '15 at 02:31
0

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.

YohanRoth
  • 1,437