views:

1060

answers:

6

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 )

+2  A: 

If (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)

Naveen
Note that this assumes the standard "mathematical" coordinate system, where Y increases upwards. In typical 2D computer graphics, that is inverted (to make Y grow with the address of the pixel).
unwind
This is only valid for a circle with center in origo.
Magnus Skog
Just add the coordinates of the centre.
Svante
A: 

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).

Slartibartfast
A: 

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).

Wikipedia has more information on how sin and cos "work".

Lucero
+6  A: 

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

adamvs
This shouldn't be voted up so high. He doesn't appear to know the value of theta. The answer by Eric Bainville is more complete.
jmucchiello
@jmucchiello - Not sure I understand you... we were asked for an algorithm and I gave the description of a function with formal parameters. At this stage, nobody knows the 'value' of theta! If you mean that I didn't calculate scaling factors for the motion of the hands of a clock, and the angle in radians for each of the hour ticks, you're absolutely right I didn't. I have work to do writing my own code!
adamvs
Yeah , I guess I asked 2 questions in one. Your answer is perfect for the first part. Thank you.
Surya
@Surya - You're very welcome... I'm glad I could help.
adamvs
+6  A: 

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);
Eric Bainville
A: 

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.

Mike Brown
It's excellent that these questions and answers helped you. That said, this section is for answers to the question only. Please don't post a comment as an answer. Regular users (those with more than 50 reputation points) can post comments underneath the question. Unfortunately, you won't be able to just yet. Feel free to answer a few questions and gain that rep so you can. :-)
Samir Talwar