PDA

View Full Version : Revilations


TJ_Tigger
09-29-2004, 08:26 PM
Well at least to me. I was working on a function to calculate the distance between two waypoints(defined by longitude and latitude) and in working in building a function found a couple of things.

1. LUA has a math.pi function built into it to define the value of pi.

2. don't try to declare a variable as a local variable twice in the same block. LUA doesn't like it. I ended up with a function where I included an if statement to figure out if the difference between two longitude measurements was greater or less than 0 and had two local declarations one in the true and false for the same variable. AMS/LUA doesn't like it. Stay away from it

Just thought I would share. Here is the function that figures distance and angle between two points on a geodisic sphere.


-- returns two values, one the distance betwen the two points and the angle between the two.
-- latitude and longitude need to be defined as radians not degrees.
function DistAngle(nlat1, nlon1, nlat2, nlon2)
local distance = 2 * Math.Asin(Math.Sqrt((Math.Sin((nlat1 - nlat2)/2))^2 + Math.Cos(nlat1)*Math.Cos(nlat2)*(Math.Sin((nlon1 -nlon2)/2))^2));
if Math.Sin(nlon2 - nlon1) < 0 then
angle = Math.Acos((Math.Sin(nlat2)-Math.Sin(nlat1) * Math.Cos(distance))/(Math.Sin(distance)*Math.Cos(nlat1)));
else
angle = 2 * pi - Math.Acos((Math.Sin(nlat2) - Math.Sin(nlat1) * Math.Cos(distance))/(Math.Sin(distance)* Math.Cos(nlat1)))
end
return distance *180*60/pi, Math.Deg(angle)
end


Tigg

BTW don't ask me about the math because I found the calulations on the web . . . somewhere.

Corey
09-29-2004, 08:52 PM
Cool post! :yes

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)

Bruce
09-29-2004, 08:58 PM
He did this during lunch breaks no less! :wow

TJ_Tigger
09-29-2004, 09:11 PM
Thanks. BTW this is what AMS did not like. :wow Debug is a good thing


function DistAngle(nlat1, nlon1, nlat2, nlon2)
local distance = 2 * Math.Asin(Math.Sqrt((Math.Sin((nlat1 - nlat2)/2))^2 + Math.Cos(nlat1)*Math.Cos(nlat2)*(Math.Sin((nlon1 -nlon2)/2))^2));
if Math.Sin(nlon2 - nlon1) < 0 then
local angle = Math.Acos((Math.Sin(nlat2)-Math.Sin(nlat1) * Math.Cos(distance))/(Math.Sin(distance)*Math.Cos(nlat1)));
else
local angle = 2 * pi - Math.Acos((Math.Sin(nlat2) - Math.Sin(nlat1) * Math.Cos(distance))/(Math.Sin(distance)* Math.Cos(nlat1)))
end
return distance *180*60/pi, Math.Deg(angle)
end

Corey
09-29-2004, 09:43 PM
He did this during lunch breaks no less!

I can easily picture Tigg carving the model for this function in his mashed potatoes like Dreyfuss in Close Encounters. :yes

I wonder if this calculation is technically considered to be bipolar? :wow

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)