Created
February 2, 2012 16:57
-
-
Save carlzulauf/1724557 to your computer and use it in GitHub Desktop.
Ruby haversine distance method, in miles
This file contains hidden or 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
def geodist(a, b) | |
rads = Math::PI/180 | |
# 7926.3352 is diameter of earth in miles | |
7926.3352 * Math.asin( | |
Math.sqrt( | |
Math.sin( (b[1] - a[1])*rads / 2 )**2 * | |
Math.cos( a[0] * rads ) * | |
Math.cos( b[0] * rads ) + | |
Math.sin( (b[0] - a[0])*rads / 2 )**2 | |
) | |
) | |
end | |
puts geodist([41.2966716,-96.0750074],[41.133382,-95.96915]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment