Skip to content

Instantly share code, notes, and snippets.

@haochi
Forked from 140bytes/LICENSE.txt
Created August 14, 2011 19:41
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 haochi/1145222 to your computer and use it in GitHub Desktop.
Save haochi/1145222 to your computer and use it in GitHub Desktop.
Haversine Formula

Haversine Formula

Haversine Formula for finding the distance between two points in a sphere.

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])
);
}
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]))}
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.
{
"name": "haversineFormula",
"description": "Haversine Formula",
"keywords": [
"haversine",
"distance",
"sphere",
"geolocation"
]
}
<!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