Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating point on a circle's circumference from angle in C#?

I imagine that this is a simple question, but I'm getting some strange results with my current code and I don't have the math background to fully understand why. My goal is simple, as stated in the title: I just want to find the point at some distance and angle from a center point.

My current code:

Point centerPoint = new Point ( 0, 0 ); Point result      = new Point ( 0, 0 ); double angle      = 0.5; //between 0 and 2 * PI, angle is in radians int distance      = 1000;  result.Y = centerPoint.Y + (int)Math.Round( distance * Math.Sin( angle ) ); result.X = centerPoint.X + (int)Math.Round( distance * Math.Cos( angle ) ); 

In general, this seems to work fairly reasonably, but I get problems at various spots, most notably when the angle corresponds to points in the negative x and y axis. Clearly I'm doing something wrong -- thoughts on what that is?

UPDATE: This was my mistake, this code works fine -- the few outliers that were not working were actually due to a bug in how the angle for 1.5PI was being calculated. I thought I had checked that well enough, but evidently had not. Thanks to everyone for their time, hopefully the working code above will prove helpful to someone else.

like image 223
Chris McElligott Park Avatar asked Mar 23 '09 16:03

Chris McElligott Park


People also ask

Which formula should be used to find the circumference of a circle C?

Circumference Formula The formula for the circumference of a circle is C=π×d, or it can be written as C=2×π×r.

How do you find the coordinates when given the radius and angle?

Typically, to find the x, y coordinates on a circle with a known radius and angle you could simply use the formula x = r(cos(degrees‎°)), y = r(sin(degrees‎°)).


1 Answers

You forgot to add the center point:

result.Y = (int)Math.Round( centerPoint.Y + distance * Math.Sin( angle ) ); result.X = (int)Math.Round( centerPoint.X + distance * Math.Cos( angle ) ); 

The rest should be ok... (what strange results were you getting? Can you give an exact input?)

like image 195
MartinStettner Avatar answered Sep 24 '22 20:09

MartinStettner