Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created August 22, 2011 19:06
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save aemkei/1163223 to your computer and use it in GitHub Desktop.
Save aemkei/1163223 to your computer and use it in GitHub Desktop.
Latitude Longitude Distance - 140byt.es

Game of Life - 140byt.es

This uses the Haversine formula to calculate the great-circle distance between two points.

Usage

distanceLatLng (
  latitude1, longitude1 // first coordinates
  latitude2, longitude2 // seconds coordinates
  diameter // earth diameter (might be any unit)
)

// returns the distance in the specified unit
```

### Earth Diameter

* 2 * 6378.137 Kilometers
* 2 * 6378137 Meter
* 2 * 3963.190 Miles


Author
------

Created by Martin Kleppe ([@aemkei](http://twitter.com/aemkei)) at [Ubilabs](http://ubilabs.net/).

For more information
--------------------

See the [140byt.es](http://140byt.es) site for a showcase of entries (built itself using 140-byte entries!), and follow [@140bytes](http://twitter.com/140bytes) on Twitter.
function(
a, b, // first coord
c, d, // second coord
e, // earth diameter (might be any unit)
z, i // placeholder
){
with (Math) { // use "Math" in context
z = PI / 360; // used to convert in radiants
return e * atan2(
sqrt(
i = pow(sin((c-a)*z), 2) +
cos(a*z*2) * cos(c*z*2) *
pow(sin((d-b)*z), 2)
),
sqrt(1-i)
);
}
}
function(a,b,c,d,e,z,i){with(Math)return z=PI/360,e*atan2(sqrt(i=pow(sin((c-a)*z),2)+cos(a*z*2)*cos(c*z*2)*pow(sin((d-b)*z),2)),sqrt(1-i))}
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": "distanceLatLng",
"description": "Calculate distance between Latitude/Longitude points.",
"keywords": [
"geo",
"maps",
"distance"
]
}
<!DOCTYPE html>
<title>Distance between Latitude/Longitude points </title>
<h4>Distance Hamburg-Tokyo</h4>
<div><b>8986</b> km expected</div>
<div><b id="km"></b> km calculated</div>
<div><b>5584</b> miles expected</div>
<div><b id="miles"></b> miles calculated</div>
<script>
var distance = function(a,b,c,d,e,z,i){with(Math)return z=PI/360,e*atan2(sqrt(i=pow(sin((c-a)*z),2)+cos(a*z*2)*cos(c*z*2)*pow(sin((d-b)*z),2)),sqrt(1-i))},
DIAMETER_IN_KM = 2 * 6378.137,
DIAMETER_IN_MILES = 2 * 3963.190,
hamburg = { lat: 53.562, lng: 9.994 },
tokyo = { lat: 35.691, lng: 139.691 };
document.getElementById( "km" ).innerHTML = Math.round(
distance(
hamburg.lat, hamburg.lng,
tokyo.lat, tokyo.lng,
DIAMETER_IN_KM
)
);
document.getElementById( "miles" ).innerHTML = Math.round(
distance(
hamburg.lat, hamburg.lng,
tokyo.lat, tokyo.lng,
DIAMETER_IN_MILES
)
);
</script>
@subzey
Copy link

subzey commented Aug 30, 2011

Great job! Flawless stereometry packed into tweet.

The only one suggestion, as the assignment has very low precedence we may re-use z variable instead of i

function(a,b,c,d,e,z){with(Math)return z=PI/360,e*atan2(sqrt(z=pow(sin((c-a)*z),2)+cos(a*z*2)*cos(c*z*2)*pow(sin((d-b)*z),2)),sqrt(1-z))}

@aemkei
Copy link
Author

aemkei commented Aug 30, 2011

Thanks for that suggestion. It saved another 2 bytes!

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