Created
May 29, 2014 09:45
-
-
Save mikz/2e0d4d94ed7aa39f3c5c to your computer and use it in GitHub Desktop.
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
local geo = {} | |
-- ~radius of the earth in miles | |
geo.radius = 3958.75587 | |
-- miles between point a and point b | |
function geo.distance(lat1, lng1, lat2, lng2) | |
local rlat = lat1*math.pi/180; | |
local rlng = lng1*math.pi/180; | |
local rlat2 = lat2*math.pi/180; | |
local rlng2 = lng2*math.pi/180; | |
if (rlat == rlat2 and rlng == rlng2) then | |
return 0 | |
else | |
-- Spherical Law of Cosines | |
return geo.radius*math.acos(math.sin(rlat)*math.sin(rlat2) | |
+math.cos(rlng-rlng2)*math.cos(rlat)*math.cos(rlat2)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment