Skip to content

Instantly share code, notes, and snippets.

@lexer
Created June 20, 2012 16:12
Show Gist options
  • Save lexer/2960718 to your computer and use it in GitHub Desktop.
Save lexer/2960718 to your computer and use it in GitHub Desktop.
var zimride = require('./zimride');
var pointA = {lat: 55.157199, lng: 61.364264};
var pointB = {lat: 55.15791, lng: 61.38864};
var pointC = {lat: 55.153472, lng: 61.364779};
var pointD = {lat: 55.153864, lng: 61.381688};
console.log(zimride.getMinDetourDistance(pointA, pointB, pointC, pointD));
var R = 6371;
Number.prototype.toRad = function () {
return this * Math.PI / 180;
}
function getDistance(point1, point2) {
lat1 = point1.lat.toRad();
lon1 = point1.lng.toRad();
lat2 = point2.lat.toRad();
lon2 = point2.lng.toRad();
return Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1)) * R;
}
/*
driver1 should get from A to B
driver2 should get from C to D
so either driver1 will pick driver2 - route ACDB
or driver2 will pickup driver1 - route CABD
since both route have common legs AC and BD, to get minimal detour distance we should compare AB and CD legs
*/
exports.getMinDetourDistance = function(pointA, pointB, pointC, pointD) {
var ab = getDistance(pointA, pointB);
var cd = getDistance(pointC, pointD);
return Math.min(ab,cd) + getDistance(pointA, pointC) + getDistance(pointB, pointD);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment