Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
up vote 5 down vote accepted


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
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? – Dave Nay Dec 14 '12 at 13:17
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
@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

Just to record a javascript adaptation from Movable Type Scripts

function createCoord(coord, bearing, distance){
     φ is latitude, λ is longitude, 
     θ is the bearing (clockwise from north), 
     δ is the angular distance d/R; 
     d being the distance travelled, R the earth’s radius*

        radius = 6371e3, //meters
        δ = Number(distance) / radius, // angular distance in radians
        θ = Number(bearing).toRad();
        φ1 = coord[1].toRad(),
        λ1 = coord[0].toRad();

    var φ2 = Math.asin(Math.sin(φ1)*Math.cos(δ) + Math.cos(φ1)*Math.sin(δ)*Math.cos(θ));

    var λ2 = λ1 + Math.atan2(Math.sin(θ)*Math.sin(δ)*Math.cos(φ1), Math.cos(δ)-Math.sin(φ1)*Math.sin(φ2));

    λ2 = (λ2+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180°

    return [λ2.toDeg(), φ2.toDeg()]; //[lon, lat]

Number.prototype.toDeg = function() { return this * 180 / Math.PI; }
Number.prototype.toRad = function() { return this * Math.PI / 180; }
share|improve this answer

Your Answer


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.