Livecode Precision or me?
Michael Doub
mikedoub at gmail.com
Sat Jan 31 15:33:34 EST 2015
I am trying to covert the javascript that computes distance based on
longitude and latitude from this page:
http://andrew.hedges.name/experiments/haversine/.
The sites javascript is returning 0.341 miles and 0.549 km
Livecode is returning 0.097 miles and 0.157
I have been staring at this for a while. Does anyone see my mistake or
is there a precision issue tripping me up?
Thanks
Mike
on doit
answer __latLongDist (38.898556, -77.037852, 38.897147, -77.043934)
end doit
function __latLongDist t1, n1, t2, n2 , mk
put 3961 into Rm -- mean radius of the earth (miles) at 39 degrees
from the equator
put 6373 into Rk -- mean radius of the earth (km) at 39 degrees
from the equator
put t1 * pi/180 into lat1 -- convert degrees to radians
put n1 * pi/180 into lon1
put t2 * pi/180 into lat2
put n2 * pi/180 into lon2
put lat2 - lat1 into dlat
put lon2 - lon2 into dlon
put (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 into aa
put 2 * atan2( sqrt(aa), sqrt(1-aa) ) into cc
put cc * Rm into Dm
put cc * Rk into Dk
return round (Dm,3), round(Dk,3)
end __latLongDist
/* main function */
function findDistance(frm) {
var t1, n1, t2, n2, lat1, lon1, lat2, lon2, dlat, dlon, a, c,
dm, dk, mi, km;
// get values for lat1, lon1, lat2, and lon2
t1 = frm.lat1.value;
n1 = frm.lon1.value;
t2 = frm.lat2.value;
n2 = frm.lon2.value;
// convert coordinates to radians
lat1 = deg2rad(t1);
lon1 = deg2rad(n1);
lat2 = deg2rad(t2);
lon2 = deg2rad(n2);
// find the differences between the coordinates
dlat = lat2 - lat1;
dlon = lon2 - lon1;
// here's the heavy lifting
a = Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) *
Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2);
c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); // great
circle distance in radians
dm = c * Rm; // great circle distance in miles
dk = c * Rk; // great circle distance in km
// round the results down to the nearest 1/1000
mi = round(dm);
km = round(dk);
// display the result
frm.mi.value = mi;
frm.km.value = km;
}
More information about the Use-livecode
mailing list