how do i find out pixel value at certain degree on the circumference of a circle if I know the pixel co-ordinates of the center of the circle, radius of the circle ,and perpendicular angle. Basically, I am trying to draw the hands of a clock at various times ( 1 o clock , 2 o clock etc )
views:
1060answers:
6If (x1,y1) is a point on the circumference and (x,y) is the center, then x1 = x + r * cos(angle)
and y1 = y + r * sin(angle)
if center is at x0, y0, and 0,0 iz at bottom-left corner, then 1 o'clock is at x0 + r*sin(2π/3), y0+r*cos(2π/3).
Draw lines from the center to coordinates computed with sin
for the y coordinates and cos
for the x coordinates (both multiplied by the length of the hand).
Where the centre of the circle is (X0, Y0), the radius is R and the angle with the x-axis is theta:
X1 = (R * cos theta) + X0 and Y1 = (R * sin theta) + Y0
Let h
be the hour as a floating point number (h=2.25
would be 02:15, etc.) between 0 and 12. (cX,cY)
are the coordinates of the center. hLength
and mLength
are the lengths of the hour and min hands.
// Hour hand
hAngle = 2.0*Pi*h/12.0; // 0..12 mapped to 0..2*Pi
hX = cX + hLength * sin(hAngle);
hY = cY - hLength * cos(hAngle);
// Min hand
mAngle = 2.0*Pi*h; // 0..1 mapped to 0..2*Pi, etc.
mX = cX + mLength * sin(mAngle);
mY = cY - mLength * cos(mAngle);
Thanks very much guys. I found this via a Google search. I'm building an analog gauge for an Android phone to show battery pack voltage and temperature, plus % charge for my electric car, and I needed some help with the math converting the angle of the ticks and the gauge needle to x,y coordinates. The source of the whole app will be posted as open source when complete.