Skip to content

Instantly share code, notes, and snippets.

@330k
Last active October 16, 2022 10:34
Show Gist options
  • Save 330k/b6d2f4a083131e7a42ee1c94003b11bf to your computer and use it in GitHub Desktop.
Save 330k/b6d2f4a083131e7a42ee1c94003b11bf to your computer and use it in GitHub Desktop.
function garmin(lat1, lon1, lat2, lon2) {
-180 == lon1 && (lon1 = 180);
var e;
var o;
var r;
var n;
var a;
var s;
var h = lat1 * Math.PI / 180;
var u = lon1 * Math.PI / 180;
var d = lat2 * Math.PI / 180;
var p = lon2 * Math.PI / 180;
var l = 6378137.0;
var f = 1 / 298.257223563;
var c = (1 - f) * l;
var y = p - u;
var m = (1 - f) * Math.tan(h);
var M = 1 / Math.sqrt(1 + m * m);
var b = m * M;
var N = (1 - f) * Math.tan(d);
var w = 1 / Math.sqrt(1 + N * N);
var v = N * w;
var x = 0;
var g = 0;
var S = 0;
var z = 0;
var E = 0;
var W = y;
var D = 0;
do {
e = Math.sin(W);
o = Math.cos(W);
r = w * e * (w * e) + (M * v - b * w * o) * (M * v - b * w * o);
if (0 == r){
break;
}
g = b * v + M * w * o;
s = W;
x = Math.sqrt(r);
n = M * w * e / x;
z = 1 - n * n;
a = f / 16 * z * (4 + f * (4 - 3 * z));
S = Math.atan2(x, g);
E = 0 != z ? g - 2 * b * v / z : 0;
W = y + (1 - a) * f * n * (S + a * x * (E + a * g * (2 * E * E - 1)));
if (Math.abs(W) > Math.PI){
throw new Error("\u03bb > \u03c0");
}
} while (Math.abs(W - s) > 1e-12 && ++D < 1e3);
if (D >= 1e3){
throw new Error("Formula failed to converge");
}
var I = z * (l * l - c * c) / (c * c);
var R = I / 1024 * (256 + I * (I * (74 - 47 * I) - 128));
var T = c * (1 + I / 16384 * (4096 + I * (I * (320 - 175 * I) - 768))) * (S - R * x * (E + R / 4 * (g * (2 * E * E - 1) - R / 6 * E * (4 * x * x - 3) * (4 * E * E - 3))));
var P = Math.atan2(w * e, M * v - b * w * o);
var k = Math.atan2(M * e, -b * w + M * v * o);
P = (P + 2 * Math.PI) % (2 * Math.PI);
k = (k + 2 * Math.PI) % (2 * Math.PI);
return {
distance: T,
initialBearing: P * 180 / Math.PI,
finalBearing: k * 180 / Math.PI,
iterations: D
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment