Haversine Formula for finding the distance between two points in a sphere.
-
-
Save haochi/1145222 to your computer and use it in GitHub Desktop.
Haversine Formula
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function (){ | |
for( | |
var m = Math // reassign Math & some of its functions | |
, c = m.cos | |
, s = m.sin | |
, a = arguments | |
, i=4; // The arguments variable is expected to have at least 4 variables. | |
// I am going to use this to count down, so when it hits 0 the loop ends. | |
i; | |
) | |
a[--i] *= m.PI/180; // converts to radian | |
return m.acos( // the glorious haversine formula | |
c(a[0])* | |
c(a[2])* | |
c(a[3]-a[1])+ | |
s(a[0])* | |
s(a[2]) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(){for(var m=Math,c=m.cos,s=m.sin,a=arguments,i=4;i;)a[--i]*=m.PI/180;return m.acos(c(a[0])*c(a[2])*c(a[3]-a[1])+s(a[0])*s(a[2]))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "haversineFormula", | |
"description": "Haversine Formula", | |
"keywords": [ | |
"haversine", | |
"distance", | |
"sphere", | |
"geolocation" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Haversine Formula</title> | |
<div>Expected value: <b>0.017453292519941554</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var myFunction = function(){for(var m=Math,c=m.cos,s=m.sin,a=arguments,i=4;i;)a[--i]*=m.PI/180;return m.acos(c(a[0])*c(a[2])*c(a[3]-a[1])+s(a[0])*s(a[2]))} | |
document.getElementById( "ret" ).innerHTML = myFunction(0,0,0,1); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment