Skip to content

Instantly share code, notes, and snippets.

@tristanls
Last active December 25, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristanls/6934102 to your computer and use it in GitHub Desktop.
Save tristanls/6934102 to your computer and use it in GitHub Desktop.
Trying to get the math right for great circle distance in JavaScript
var NVector = module.exports = function NVector (x, y, z) {
var self = this;
self.x = x;
self.y = y;
self.z = z;
};
NVector.distance = function distance (a, b) {
// calculate magnitude of a x b (cross product)
var crossProduct = new NVector(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
var crossProductMagnitude = Math.sqrt(
Math.pow(crossProduct.x, 2) +
Math.pow(crossProduct.y, 2) +
Math.pow(crossProduct.z, 2));
// calculate dot product
var dotProduct = a.x * b.x + a.y * b.y + a.z * b.z;
// great circle distance in km (earth radius 6371km)
return Math.atan2(crossProductMagnitude, dotProduct) * 6371;
};
NVector.fromLatLon = function fromLatLon (latitude, longitude) {
latitude = latitude * 0.0174532925; // degrees to radians
longitude = longitude * 0.0174532925; // degrees to radians
return new NVector(
Math.sin(latitude),
Math.sin(longitude) * Math.cos(latitude),
-1 * Math.cos(longitude) * Math.cos(latitude));
};
// inline tests
var v1 = NVector.fromLatLon(31.565665, -96.811523);
console.log('v1', v1.x, v1.y, v1.z);
var v2 = NVector.fromLatLon(31.465952, -97.026443);
console.log('v2', v2.x, v2.y, v2.z);
console.log('v1 and v2', NVector.distance(v1, v2));
var v3 = NVector.fromLatLon(28.204280, -99.283447);
console.log('v3', v3.x, v3.y, v3.z);
console.log('v1 and v3', NVector.distance(v1, v3));
console.log('v2 and v3', NVector.distance(v2, v3));
console.log('v3 and v3', NVector.distance(v3, v3));
var v4 = NVector.fromLatLon(39.492384, -123.178711);
console.log('v4', v4.x, v4.y, v4.z);
console.log('v1 and v4', NVector.distance(v1, v4));
console.log('v2 and v4', NVector.distance(v2, v4));
console.log('v3 and v4', NVector.distance(v3, v4));
var v5 = NVector.fromLatLon(-0.381772, -179.736328);
var v6 = NVector.fromLatLon(0.381772, 179.736328);
console.log('v5 and v6', NVector.distance(v5, v6));
var v7 = NVector.fromLatLon(0, 0);
var v8 = NVector.fromLatLon(0, 180);
console.log('0,0 and 0,180', NVector.distance(v7, v8));
var v9 = NVector.fromLatLon(90, 0);
console.log('0,0 and 90,0', NVector.distance(v7, v9));
var v10 = NVector.fromLatLon(90, 180);
console.log('0,0 and 90,180', NVector.distance(v7, v10));
var v11 = NVector.fromLatLon(50.474987, 20.214844);
var v12 = NVector.fromLatLon(50.474982, 20.213621);
console.log('very close', NVector.distance(v11, v12));
var v13 = NVector.fromLatLon(-90, 0);
console.log('90, 0 and -90, 0', NVector.distance(v9, v13));
var v14 = NVector.fromLatLon(90, -1);
console.log('90, 0 and 90, -1', NVector.distance(v9, v14));
@tristanls
Copy link
Author

To run: ~$ node nVectorDistance.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment