csbrandt / WGS84Util

Geographic coordinate utilities using WGS84 datum
13 stars 3 forks source link

add `bearingBetween(coordA, coordB)` #2

Closed derhuerst closed 8 years ago

derhuerst commented 9 years ago

I'd like to get the bearing from coordA & coordB and use it to calculate the position on a "2D" map.

Right now, the library allows me to do coordA, bearing, distance -> coordB, but doing coordA, coordB -> bearing, distance is impossible.

astro commented 9 years ago

To obtain the distance use the distanceBetween() function.

If you can assume that the planet is an ideal sphere, bearing is as simple as the following:

Math.atan2(p[1].lat - p[0].lat, p[1].lon - p[0].lon)

I hope that helps for now. Of course it'd be totally awesome if the library provided this functionality beyond the ideal sphere assumption.

derhuerst commented 9 years ago

I hope that helps for now. Of course it'd be totally awesome if the library provided this functionality beyond the ideal sphere assumption.

But the whole point of the library is to handle a non-ideal sphere, right?

csbrandt commented 8 years ago

@astro @derhuerst

But the whole point of the library is to handle a non-ideal sphere, right?

Correct, that is the assumption with this library. After finding some information on a great circle path for initial bearings at http://www.movable-type.co.uk/scripts/latlong.html realized that these calculations do not take into account the ellipsoid. Such a solution would not be suitable here.

Found a brilliant implementation of the Inverse problem described and coded in JavaScript here

This solution involves iteration over a handful of trigonometric functions so performance will be slower than your basic great circle implementations and actually I am re-evaluating the use of the Haversine formula for calculating distance after finding this.

Since we are taking into account the wgs-84 ellipsoid the preference is leaning towards accuracy over performance IMO, should have some updates soon as I now have a need for a bearingBetween() function

csbrandt commented 8 years ago

fixed by e220fec355161d76bcb3a723efd998b5844e6ad6