3

I found the following answer on Stackoverflow.

From what I understand, this is not the proper way to do it. The math is linear, however, the coordinates are mapped to a spherical surface. So what is the proper way to do it?

I have a function that calculates a midpoint, how can I alter it to accept a percentage as a parameter. In other words, find a midpoint that is between point 1 and 2 and is a percentage away from point 1...

middle_point(lat1, long1, lat2, long2) {

    // Longitude difference.
    var d_long = (long2 - long1) * Math.PI / 180;

    // Convert to radians.
    lat1 = lat1 * Math.PI / 180;
    lat2 = lat2 * Math.PI / 180;
    long1 = long1 * Math.PI / 180;

    var b_x = Math.cos(lat2) * Math.cos(d_long);
    var b_y = Math.cos(lat2) * Math.sin(d_long);

    var lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + b_x) * (Math.cos(lat1) + b_x) + b_y * b_y)); 
    var long3 = long1 + Math.atan2(b_y, Math.cos(lat1) + b_x);

    // Return result.
    return [long3 * 180/Math.PI, lat3 * 180/Math.PI];

}
buydadip
  • 8,890
  • 22
  • 79
  • 154
  • This seems less programming-y and more mathematics-y? – Anthony Sep 14 '18 at 01:25
  • Probably the mathematic answer about this subject can be found here: https://math.stackexchange.com/questions/35990/formula-for-the-coordinate-of-the-midpoint-in-spherical-coordinate-system – Thiago Mata Sep 14 '18 at 02:53
  • This may even be more close to the coding stage https://www.mathworks.com/matlabcentral/answers/229312-how-to-calculate-the-middle-point-between-two-points-on-the-earth-in-matlab – Thiago Mata Sep 14 '18 at 02:55

1 Answers1

1

The answer is given here: http://www.movable-type.co.uk/scripts/latlong.html and here: http://www.edwilliams.org/avform.htm#Intermediate

Here is the function you need, with "perc" between 0 and 100:

intermediate_point(lat1, long1, lat2, long2, perc) {

    // Normalize percentage
    perc = perc / 100;

    // Convert to radians.
    lat1 = lat1 * Math.PI / 180;
    lat2 = lat2 * Math.PI / 180;
    long1 = long1 * Math.PI / 180;
    long2 = long2 * Math.PI / 180;

    // get angular distance between points
    var d_lat = lat2 - lat1;
    var d_lon = long2 - long1;
    var a = Math.sin(d_lat/2) * Math.sin(d_lat/2) + Math.cos(lat1) * 
        Math.cos(lat2) * Math.sin(d_lon/2) * Math.sin(d_lat/2);

    var d = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    var A = Math.sin((1-perc)*d) / Math.sin(d);
    var B = Math.sin(perc*d) / Math.sin(d);

    var x = A * Math.cos(lat1) * Math.cos(long1) + B * cos(lat2) * cos(long2);
    var y = A * Math.cos(lat1) * Math.sin(long1) + B * cos(lat2) * sin(long2);
    var z = A * Math.sin(lat1) + B * Math.sin(lat2);

    var lat3 = Math.atan2(z, Math.sqrt(x*x + y*y));
    var long3 = Math.atan2(y, x);

    // Return result, normalising longitude to -180°...+180°
    return [long3 * 180/Math.PI, (lat3 * 180/Math.PI + 540)%360-180];
}