views:

589

answers:

4

Using C#:

How do I get the (x, y) coordinates on the edge of a circle for any given degree, if I have the center coordinates and the radius?

There is probably SIN, TAN, COSIN and other grade ten math involved... :)

+16  A: 

This has nothing to do with C#. There is just some elementary mathematics involved.

x = x0 + r * cos(theta)
y = y0 + r * sin(theta)

theta is in radians, x0 and y0 are the coordinates of the centre, r is the radius, and the angle is measured anticlockwise from the x-axis. But if you want it in C#, and your angle is in degrees:

double x = x0 + r * Math.Cos(theta * Math.PI / 180);
double y = y0 + r * Math.Sin(theta * Math.PI / 180);
David M
+1, for speedy-easy votes :P
AnthonyWJones
I feel ashamed accepting the upvotes on this to be honest.
David M
It funny on these types of questions how almost identical the answers are. Even down to the structure of the answer :P
Alastair Pitts
@tm1rbrt - The degrees to radians conversion is in my code already.
David M
1 radian ~= 57.3 degrees (57.298 if I'm being "precise"). Somehow this number comes to mind quicker for me than 180/pi.
phkahler
@phkahler. Hmmm - you're on your own there!
David M
There's a pretty critical typo in the actual code sample. Note that it doesn't match the algebraic expressions!
Pillsy
Pillsy: Yes, the Math.Cos() should have been a Math.Sin(). That didn't deserve a downvote in my opinion. I've fixed it.
Daniel Vassallo
Thanks for that Daniel.
David M
+1  A: 

For a circle with origin (j, k), radius r, and angle t in radians:

   x(t) = r * cos(t) + j       
   y(t) = r * sin(t) + k
Daniel Vassallo
+4  A: 

http://en.wikipedia.org/wiki/Trigonometry is your friend, the calculations are quite simple.

Check the Math Namespace for Sin() & Cos()

dbemerlin
+3  A: 

using Pythagoras Theorem (where x1,y1 is the edge point):

x1 = x + r*cos(theta)
y1 = y + r*sin(theta)

in C#, this would look like:

x1 = x + radius * Math.Cos(angle * (Math.PI / 180));
y1 = y + radius * Math.Sin(angle * (Math.PI / 180));

where all variables are doubles and angle is in degrees

Alastair Pitts