File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programming Diversions and the fly likes Clock problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Clock problem" Watch "Clock problem" New topic

Clock problem

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Find formula to compute angle between hour hand and minute hand.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11913

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

but how?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
More info available here.

"I'm not back." - Bill Harding, Twister
Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1893
Originally posted by Pradeep Bhat:
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 ]

Pravin Mhaske

Joined: Jan 17, 2005
Posts: 2
Originally posted by Pradeep Bhat:
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

Joined: Nov 13, 2003
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

Joined: Dec 06, 2001
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.


Java API Documentation
The Java Tutorial
Pravin Mhaske

Joined: Jan 17, 2005
Posts: 2
Hi P. Sangdeo, Why do you think your answer is not complicated? I don't understand 'C' or whatever the language is Would you pls give a simple mathematical formula? Also, can you please prove that the formula you've posted gives correct answer with an example? You may please refer to the above post of mine for the example.
I agree. Here's the link:
subject: Clock problem
It's not a secret anymore!