Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Okay, say I have a point coordinate.

var coordinate = { x: 10, y: 20 };

Now I also have a distance and an angle.

var distance = 20;
var angle = 72;

The problem I am trying to solve is, if I want to travel 20 points in the direction of angle from the starting coordinate, how can I find what my new coordinates will be?

I know the answer involves things like sine/cosine, because I used to know how to do this, but I have since forgotten the formula. Can anyone help?

share|improve this question
1  
72 degrees from what? The X-axis, the Y-axis? Something else? Clockwise, anticlockwise? –  pdr Dec 14 '12 at 1:59
    
@pdr 90 degrees would be a direction of north, 45 degrees would be a direction of north east, etc. –  dqhendricks Dec 14 '12 at 2:12
add comment

1 Answer 1

up vote 4 down vote accepted

SOHCAHTOA

Sine = Opposite/Hypotenuse Cosine = Adjacent/Hypotenuse Tangent = Opposite/Adjacent

In your example:

Sine(72) = Y/20 -> Y = Sine(72) * 20
Cosine(72) = X/20 -> X = Cosine(72) *20

The problem is you have to be careful with what quadrant you are in. This works perfectly in the upper right quadrant, but not so nice in the other three quadrants.

share|improve this answer
1  
This works in all quadrants. The full formula for rotating a vector (X,Y) is X'= X * sin(angle) + Y * cos(angle) and Y'= X * sin(angle) + Y * -cos(Angle). This simplifies to what you have above when just rotating from the x axis (1,0). –  Chewy Gumball Dec 14 '12 at 10:10
    
Hmmm...what transform am I remembering that has a gotcha regarding the quadrants? –  DaveNay Dec 14 '12 at 13:17
2  
Note that in javascript, Math.sin and the like takes input in radians, so you will need to convert: radians = (degrees * (Math.PI/180) –  Brian Dec 14 '12 at 14:15
1  
@DaveNay you have problems when doing the Arc functions. Sin(45degrees)=Sin(135degrees) therefore arcsin(sin(135degrees)) will return 45degrees; Cos(45)=Cos(315)... –  mhoran_psprep Dec 14 '12 at 15:16
    
thanks alot guys –  dqhendricks Dec 14 '12 at 17:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.