chrisveness / geodesy

Libraries of geodesy functions implemented in JavaScript
http://www.movable-type.co.uk/scripts/geodesy-library.html
MIT License
1.17k stars 203 forks source link

Cross track distance and along track distance to starting point gives `NaN` instead of expected `0` #76

Closed agwells closed 4 years ago

agwells commented 4 years ago

Thanks for sharing this great library! :)

I've noticed that using the LatLonSpherical class's this.alongTrackDistanceTo() and this.crossTrackDistanceTo(), if the this point is the same location as the starting point parameter, the methods return NaN rather than the expected 0.

Example:

import LatLonSpherical from 'geodesy/latlon-spherical';

const p1 = new LatLonSpherical(10, 20);
const p1a = new LatLonSpherical(10, 20);

const pB = new LatLonSpherical(11, 21);

console.log(p1.distanceTo(p1a)); // 0
console.log(p1.alongTrackDistanceTo(p1a, pB)); // NaN
console.log(p1.crossTrackDistanceTo(p1a, pB)); // NaN

Expected result:

I expected both operations to return numeric 0 rather than NaN. This makes sense intuitively, and it would be consistent with the behavior of the .distanceTo() method, which returns a distance of 0 from one location to the same location.


console.log(p1.distanceTo(p1a)); // 0
chrisveness commented 4 years ago

You are quite correct!

Thank you, I will fix that.

agwells commented 4 years ago

I also tried LatLonNvectorSpherical. It also returns NaN for crossTrackDistance(), while alongTrackDistance() returns a very small non-zero float.

import LatLonNvectorSpherical from 'geodesy/latlon-nvector-spherical';

const p1 = new LatLonNvectorSpherical(10, 20);
const p1a = new LatLonNvectorSpherical(10, 20);

const pB = new LatLonNvectorSpherical(11, 21);

console.log(p1.distanceTo(p1a)); // 0
console.log(p1.alongTrackDistanceTo(p1a, pB)); // 6.799437946888028e-10
console.log(p1.crossTrackDistanceTo(p1a, pB)); // NaN
chrisveness commented 4 years ago

Fixed in v2.2.1.