Tutorial :Find coordinate by angle



Question:

I am developing in application in XNA which draws random paths. Unfortunately, I'm out of touch with graphing, so I'm a bit stuck. My application needs to do the following:

  1. Pick a random angle from my origin (0,0), which is simple.
  2. Draw a circle in relation to that origin, 16px away (or any distance I specify), at the angle found above.

(Excuse my horrible photoshoping)

alt text http://www.refuctored.com/coor.png

The second circle at (16,16) would represent a 45 degree angle 16 pixels away from my origin.

I would like to have a method in which I pass in my distance and angle that returns a point to graph at. i.e.

private Point GetCoordinate(float angle, int distance)  {     // Do something.     return new Point(x,y);  }  

I know this is simple, but agian, I'm pretty out of touch with graphing. Any help?

Thanks, George


Solution:1

If the angle is in degrees, first do:

angle *= Math.PI / 180;  

Then:

return new Point(distance * Math.Cos(angle), distance * Math.Sin(angle));  

By the way, the point at (16, 16) is not 16 pixels away from the origin, but sqrt(16^2 + 16^2) = sqrt(512) =~ 22.63 pixels.


Solution:2

private Point GetCoordinate(float angle, int distance)  {    float x = cos(angle) * distance;    float y = sin(angle) * distance;    return new Point(x, y);  }  

Note that the trigonometric functions probably take radians. If your angle is in degrees, divide by 180/Pi.


Solution:3

in general:

x = d * cos(theta)  y = d * sin(theta)  

Where d is the distance from the origin and theta is the angle.


Solution:4

Learn the Pythagorean Theorem. Then this thread should have more specific details for you.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »