Clock Hands Accuracy

Back to General discussions forum

DaskieLQC     2015-05-08 06:03:45

I believe the accuracy of the checker may be off on the Clock Hands question. I've solved the logic/math of the problem, and have been trying for awhile now to just get the accuracy of my answers to match that of the answer. That is, to be less precise.

An example

Input data:
5
18:43 17:18 23:52 00:26 20:54

Checker's answer:
7.80099264 4.41749459 1.19667159 8.12879478
12.15020770 4.39851744 18.55950865 7.21884705
9.58146116 15.98538430 3.31169657 16.02217546
11.34970633 15.84622039 13.66062979 1.77809088
4.00822279 9.68598426 4.70993273 17.28115295

My answer:
9.99999974 4.00000000 1.19667144 8.12879550
12.99999985 4.80384749 18.55950862 7.21884698
7.00000061 15.19615277 3.31169722 16.02217618
9.99999974 16.00000000 13.66062952 1.77809076
4.80384736 7.00000038 4.70993356 17.28115355

As you can see, they match for the most part. However, they diverge quite significanly for some values. These answers were generated using C/C++'s M_PI constant: 3.14159265358979323846

I have verified the values via calculator and the answers much more closely match my own. The code I'm using is below. Hopefully I'm just doing something wrong, but I appreciate any feedback :)

theta = hours % 12 / 12.0 * 2 * M_PI;       //convert 24 hour format to radians
theta = -theta + M_PI / 2;                  //adjust angle so clock hands face +y and rotate cw
x = cos(theta) * 6 + 10;                    //calculate x and y coord and center it at (10, 10)
y = sin(theta) * 6 + 10;                    //6 is length of hour hand
cout << x << " " << y << " ";

theta = minutes / 60.0 * 2 * M_PI;          //convert 60 minute format to radians
theta = -theta + M_PI / 2;                  //^
x = cos(theta) * 9 + 10;                    //^
y = sin(theta) * 9 + 10;                    //9 is length of minute hand
cout << x << " " << y << " ";
DaskieLQC     2015-05-08 06:34:00

Here are the answers calculated externally:
10.00000000 4.00000000 1.19667000 8.12879000
13.00000000 4.80385000 18.55950000 7.21885000
7.00000000 15.19620000 3.31170000 16.02220000
10.00000000 16.00000000 13.66060000 1.77809000
4.80385000 7.00000000 4.70993000 17.28120000

Quandray     2015-05-08 09:22:57
User avatar

Your answers for the minute hand are good but your answers for the hour hand are way off.

Your calculation for the hour hand would give the same answer for 00:00 as for 00:59, but the hour hand would have moved by nearly 30 degrees.

DaskieLQC     2015-05-08 11:38:57

Thank you! Not sure how I didn't realize that earlier.

Updated Code:
theta = (hours % 12 + minutes / 60.0) / 12.0 * 2 * PI;

Please login and solve 5 problems to be able to post at forum