Re: Best way to...
- Posted by irv Oct 17, 2010
- 1745 views
Well, if you know how to phrase the question so it will fit on the subject line, please don't keep us in suspense.
Thanks for the suggestions. The following code, copied from a javascript version, works and gives a reasonably accurate distance, as well. This takes slightly more than 1 second on my computer, which is acceptable.
include std/math.e include std/sequence.e include std/get.e include std/io.e include std/sort.e constant R = 6371 -- km Radius of earth enum ID,LON,LAT,KM atom lat1, lat2, lon1, lon2, dLat, dLon, a, c, km, nm, sm -- current location (KLAX) lat1 = 33.9425346884195 lon1 = -118.408074361323 sequence airports = read_lines("index.txt") for i = 1 to length(airports) do airports[i] &= "|0" -- place to store distance in km airports[i] = split(airports[i],'|') lat2 = defaulted_value(airports[i][LAT],0) lon2 = defaulted_value(airports[i][LON],0) dLat = deg2rad(lat2-lat1) dLon = deg2rad(lon2-lon1) a = sin(dLat/2) * sin(dLat/2) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * sin(dLon/2) * sin(dLon/2) c = 2 * atan2(sqrt(a), sqrt(1-a)) km = R * c --nm = km * 0.5399568034557235 --sm = km * 0.621371192237334 airports[i][KM] = km end for airports = sort_columns(airports,{KM}) for i = 1 to 3 do printf(1,"%s %.2f km\n",{airports[i][ID],airports[i][KM]}) end for