Created
April 11, 2012 23:40
-
-
Save andersosthus/2363499 to your computer and use it in GitHub Desktop.
Ruby Haversine
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
def haversine(lat1, long1, lat2, long2) | |
dtor = Math::PI/180 | |
r = 6378.14*1000 | |
rlat1 = lat1 * dtor | |
rlong1 = long1 * dtor | |
rlat2 = lat2 * dtor | |
rlong2 = long2 * dtor | |
dlon = rlong1 - rlong2 | |
dlat = rlat1 - rlat2 | |
a = power(Math::sin(dlat/2), 2) + Math::cos(rlat1) * Math::cos(rlat2) * power(Math::sin(dlon/2), 2) | |
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a)) | |
d = r * c | |
return d | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment