Find angle between hour and minute hands in an analog clock
It turns out that Wikipedia does have the best answer:
// h = 1..12, m = 0..59
static double angle(int h, int m) {
double hAngle = 0.5D * (h * 60 + m);
double mAngle = 6 * m;
double angle = Math.abs(hAngle - mAngle);
angle = Math.min(angle, 360 - angle);
return angle;
}
Basically:
- The hour hand moves at the rate of
0.5
degrees per minute - The minute hand moves at the rate of of
6
degrees per minute
Problem solved.
And precision isn't a concern because the fractional part is either .0
or .5
, and in the range of 0..360
, all of these values are exactly representable in double
.
For finding the angle between the hands of a clock is ,
30 * [HRS - (MIN/5)] + (MIN/2)