12

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?

  • 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
5

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.

  • 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? – Dave Nay 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
2

Just to record a javascript adaptation from Movable Type Scripts

function createCoord(coord, bearing, distance){
    /** http://www.movable-type.co.uk/scripts/latlong.html
     φ 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*
     **/

    var 
        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; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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