8

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 )

rtn
  • 127,556
  • 20
  • 111
  • 121
Surya
  • 4,922
  • 8
  • 41
  • 54

5 Answers5

19

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
  • 9,738
  • 1
  • 25
  • 27
9

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
avstrallen
  • 986
  • 9
  • 9
  • 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 May 29 '09 at 12:59
  • @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! – avstrallen May 29 '09 at 16:44
  • 1
    Yeah , I guess I asked 2 questions in one. Your answer is perfect for the first part. Thank you. – Surya May 29 '09 at 19:19
4

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
  • 74,600
  • 47
  • 176
  • 233
  • 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 May 29 '09 at 10:01
  • This is only valid for a circle with center in origo. – rtn May 29 '09 at 10:42
  • Just add the coordinates of the centre. – Svante May 29 '09 at 11:26
0

if center is at x0, y0, and 0,0 iz at bottom-left corner, then 1 o'clock is at x0 + rsin(2π/3), y0+rcos(2π/3).

Slartibartfast
  • 8,735
  • 6
  • 41
  • 45
-1

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
  • 59,176
  • 9
  • 122
  • 152