Thursday, October 14, 2010
Analog clock Algorithm
All these kind of algorithms draw lines from a point (center of your clock) with different length/width/color (hour arm, minute arm, second arm), towards different angle (different time).
Assume your clock center is at (x,y), the length of the arm is r, and the angle is a, then you need to calculate a second point (x1, y1) so you can draw a line between (x,y) and (x1, y1) to paint a clock arm.
If you have difficulties with 'sin' and 'cos', you can just use the following equations as formulas
x1 = x + r*sin(a);
y1 = y - r*cos(a);
NOTE about angle "a". In above formula angle a = 0 if the arm point straing up (at 12 clock)
Now how to get these angles for different arms
for second arm, it's a matter of mapping 0-60 to 0 - 360. This is easy, just multiple the second value by 6. (Each second the arm will rotate 6 degree, you get 15 second = 90 degree, 30 second = 180 degree, etc.)
Minute arm is the same as second arm, as it's also mapping 0-60 to 0-360
for hour arm is less straightforward, as analog clock programs usually don't map 0-12 to 0-360 - very often you notice the hour arm is pointing in the middle of two readings, incidating the time is between the two hours. In this case the mapping is 0-720 (minutes) to 0-360. The mapping is easy (divide by 2). This means for each minute passes, the hour arm rotates half degree. At this speed after 12 hours (720 minutes) the hour arm will complete a circle.
At last, you'll need to convert degree to radain as it's what requested by C#. To convert angle a to radian, you can use formula
a = a * Math.PI / 180
then plug a to the formula to calculate (x1, y1)
SOURCE
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment