While reading the bible for programmers 'Cracking the Coding Interview', I came across an interesting problem of determining the angle between the hour and the minutes hands of an analog clock, given a time.
This drew my attention and I began looking into it. Here is a small explanation about the approach to solve this problem.
What we would need to do is to calculate the rate of change of angle in degrees per minute. The first step to take is to recall that hour hand can turn 360 degree in 12 hours ( that is 12 * 60 = 720 mins)
Rate of change of angle for the hour hand is = 360/720 = 0.5 degrees per minute
Similarly, the minute hand of the clock turns 360 degrees in 60 minutes
So rate of change of angle for the minute hand is = 360/60 = 6 degrees per minute
For a given time X hrs and Y mins
Angle for hour X = 0.5 * ( X *60 + Y)
Angle for minutes Y = 6 * Y
We want to calculate the angle between the hour and the minute hand. So we should be interested in calculating the difference between Angle for hour X and Angle for minutes Y
Difference of Angle between X hrs and Y mins is Z = 0.5 * ( X *60 + Y) - 6 * Y
The angle between the hour and the minute hand will be the smaller of (360- Z, Z)
This drew my attention and I began looking into it. Here is a small explanation about the approach to solve this problem.
What we would need to do is to calculate the rate of change of angle in degrees per minute. The first step to take is to recall that hour hand can turn 360 degree in 12 hours ( that is 12 * 60 = 720 mins)
Rate of change of angle for the hour hand is = 360/720 = 0.5 degrees per minute
Similarly, the minute hand of the clock turns 360 degrees in 60 minutes
So rate of change of angle for the minute hand is = 360/60 = 6 degrees per minute
For a given time X hrs and Y mins
Angle for hour X = 0.5 * ( X *60 + Y)
Angle for minutes Y = 6 * Y
We want to calculate the angle between the hour and the minute hand. So we should be interested in calculating the difference between Angle for hour X and Angle for minutes Y
Difference of Angle between X hrs and Y mins is Z = 0.5 * ( X *60 + Y) - 6 * Y
The angle between the hour and the minute hand will be the smaller of (360- Z, Z)
No comments:
Post a Comment