Lua Trigonometry

306 views Asked by At

Currently I am trying to get the bearing from the first latitude-longitude point to the last.

I have been to the movable-type website and have got my distance to work but my bearing seems to be giving me trouble.

Y = math.sin(dLon_WPM_1) * math.cos(lat2_WPM_1)
X = math.cos(dLat_WPM_1) * math.sin(lat2_WPM_1) 
    - math.sin(dLat_WPM_1) * math.cos(lat2_WPM_1) * math.cos(dLon_WPM_1)
BRNG = math.deg(math.atan2(Y, X))

I have double checked and triple checked: all the values are the values I think they are.

Thanks for the help!

2

There are 2 answers

0
Alex On

Where you have dLat_WPM_1 in the calc for X, try using lat1_WPM_1 instead.

0
Pekka On

Have you considered the effect of spherical coordinates? The bearing you are attempting to calculate would only be correct if you had projected the points onto something like a (bearing preserving) Mercator projection and then calculated the rhumbline bearing.

If you are, instead, interested in the initial great circle bearing, you can look at the blog posting at Calculate distance, bearing and more between Latitude/Longitude points for the equations.