chrisveness/geodesy

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

agwells opened this issue · 3 comments

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

You are quite correct!

Thank you, I will fix that.

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

Fixed in v2.2.1.