13

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?

2
  • 3
    72 degrees from what? The X-axis, the Y-axis? Something else? Clockwise, anticlockwise?
    – pdr
    Dec 14, 2012 at 1:59
  • @pdr 90 degrees would be a direction of north, 45 degrees would be a direction of north east, etc. Dec 14, 2012 at 2:12

3 Answers 3

8

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.

4
  • 2
    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). Dec 14, 2012 at 10:10
  • Hmmm...what transform am I remembering that has a gotcha regarding the quadrants?
    – Dave Nay
    Dec 14, 2012 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, 2012 at 14:15
  • 2
    @DaveNay you have problems when doing the Arc functions. Sin(45degrees)=Sin(135degrees) therefore arcsin(sin(135degrees)) will return 45degrees; Cos(45)=Cos(315)... Dec 14, 2012 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; }
-1

i fount the equation here, i implemented it and it works perfectly.

this is my simple :

    const R = 6371e3; //rayon of the erth
    let lat1 = 36.7538 * Math.PI / 180; // latitude in rad
    let long1 = 3.0588 * Math.PI / 180; // longiture in rad
    let d = 5000; //distance between the two points
    let angle = (90-20)*Math.PI/180; // the angle between the 2 points in rad (20°)
    const sigma = d / R;
    const delLat = sigma * Math.cos(angle);
    let lat2 = (lat1 + delLat) * 180 / Math.PI;//latitude of the destination point in deg
    const del = Math.log(Math.tan(lat2 / 2 + Math.PI / 4) / Math.tan(lat1 / 2 + Math.PI / 4));
    // const q = Math.abs(del) > 10e-12 ? delLat / del : Math.cos(lat1); 
    const q = Math.cos(lat1)
    
    const delLong = sigma * Math.sin(angle) / q;
    let long2 = (long1 + delLong) * 180 / Math.PI; //longitude of the destination point in deg

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