distance point to point revisited
- Posted by Senator
Jan 30, 2018
--
-- distance.e
--
include std/math.e
-- Great Circle Method
global function distance(atom LA1, atom LO1, atom LA2, atom LO2, integer miles = 1)
-- given starting and destination lattitude and longitude coordinates in degrees
-- returns distance between the two points (default: miles = 1, or km (miles = 0)
-- South latitudes are negative / East longitudes are positive
-- uses spherical law of cosines
atom D, DLO, R = 6371.02 -- mean earth radius in km
DLO = deg2rad(LO2-LO1)
LA1 = deg2rad(LA1) LO1 = deg2rad(LO1)-- initial location coordinates
LA2 = deg2rad(LA2) LO2 = deg2rad(LO2)-- destination location coordinates
D = arccos( sin(LA1) * sin(LA2) + cos(LA1) * cos(LA2) * cos(DLO)) * R
if miles then
D = D / 1.609 -- convert km to miles
end if
return D
end function
-- test
? distance(41.3, 174.7, 37.0, 174.7) -- Wellington-Auckland
? distance(41.3, 174.7, 33.7, 151.3) -- Wellington-Sydney
atom LA1, LA2, LO1, LO2
LA1 = 32.204287 LO1 = 82.322732 -- Lyons, GA -- starting location
LA2 = 51.5074 LO2 = 0.1278 -- London, UK
? distance(LA1,LO1, LA2, LO2)
LA2 = 33.7490 LO2= 84.3880 -- Lyons, GA to Atlanta, GA
? distance(LA1,LO1,LA2,LO2)
LA2 = 32.8407 LO2 = 83.6324 -- Lyons, GA to Macon, GA
? distance(LA1,LO1,LA2,LO2)
LA2 = 32.2177 LO2 = 82.4135 -- Lyons, GA to Vidalia, GA
? distance(LA1,LO1,LA2,LO2)
LA2 = 32.0522 LO2 = 118.2437 -- Lyons, GA to Los Angeles, California
? distance(LA1,LO1,LA2,LO2)
Not Categorized, Please Help
|
|