Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Clock problem

Ranch Hand
Posts: 8927
Find formula to compute angle between hour hand and minute hand.

fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
it's trivial to figure out each hands postion, given the time. you can calculate the angle around from 12:00:00.
if you know each hands postion, the angle between them is the difference.

Ranch Hand
Posts: 8927
but how?

Jim Yingst
Wanderer
Sheriff
Posts: 18671

Arjun Shastry
Ranch Hand
Posts: 1898
1
but how?

Input is some time ,like 5:37.How much is angular movement of hour hand from position 12? 30*5 = 150 degrees + 15 degrees(bcos of those first 30 minutes) = 165 degrees.
Now hour hand moves by 30 degrees when minutes hand moves by 360 degrees,right? so for 1 minute,30/360-->(1/12) degrees,correct?So for 7 minutes 7/12 degrees.So total angular movement of hour hand is 165+(7/12) degrees.
Now angular movement of minute hand is 180+30+12 = 222 degrees.
I hope now you can make a formula out of it.
Has anybody has used Theodolite for calculating the angle and the distance between the two objects on a surface?
[ November 25, 2003: Message edited by: Capablanca Kepler ]

Greenhorn
Posts: 2
Find formula to compute angle between hour hand and minute hand.

angle = 30H - 5.5M
Example, at 2:35, angle = 30*2 - 5.5*35 = 60 - 192.5 = 132.5 degrees (ignore the sign)
[ January 17, 2005: Message edited by: Pravin Mhaske ]

P. Sagdeo
Ranch Hand
Posts: 67
I think that what has been posted so far is too complicated. All that is needed is to know that 1 minute should be 6 degrees (360 degrees in 60 minutes, so that follows). So when you have the minutes, to compute the degrees, you simply do:

Hours can be either much easier or a little harder. First the easy way:
Since you know there are 12 hours in 360 degrees, the difference between hours is simple 360/12 = 30 degrees. So the code is:

However, if you wanted to be like a real clock, where the hour hand slowly moves continuously over the hour, then you would have to take into account the amount of minutes that have passed. To do this, I would simply find the decimal between the hours (i.e. 6:30 would be 6.5) and then find the degrees. The code then would be something like:

Now that we have the two degree values, we can subtract their values. This can be done like:

Layne Lund
Ranch Hand
Posts: 3061
If minutes is an int, then

will not give the correct result as described above. This line of code should be changed to

in order to avoid integer division.

Layne