Can someone identify this algorithm for great-circle distance?

204 Views Asked by At

The below is an algorithm used by the jscoord library to calculate the distance between 2 coordinates:

function LatLngDistance(to) {
  var er = 6366.707;

  var latFrom = deg2rad(this.lat);
  var latTo   = deg2rad(to.lat);
  var lngFrom = deg2rad(this.lng);
  var lngTo   = deg2rad(to.lng);

  var x1 = er * Math.cos(lngFrom) * Math.sin(latFrom);
  var y1 = er * Math.sin(lngFrom) * Math.sin(latFrom);
  var z1 = er * Math.cos(latFrom);

  var x2 = er * Math.cos(lngTo) * Math.sin(latTo);
  var y2 = er * Math.sin(lngTo) * Math.sin(latTo);
  var z2 = er * Math.cos(latTo);

  var d = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));

  return d;
}

Could someone identify the algorithm and if possible provide a link to a discussion / explanation?

Starter for 10: It's not Haversine :-)

1

There are 1 best solutions below

4
On BEST ANSWER

The routine computes the 3D coordinates of the points on the Earth surface corresponding to the given coordinates. Then it simply uses Pythagoras to compute the 3D distance. That is, the result is not the distance along a great circle (on the Earth surface), but rather the straight distance (as if you dig a tunnel). Of course the formula could be simplified by removing all multplications with $er$ and only multiply the final result with $er$ (saves five multiplications).