-
-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getDistanceFromLine is returning NaN #227
Comments
I had a similar issue - in my case, I rewrote the method to account for inputs being the same point (division by 0 errors are not accounted for in the current implementation). |
It happens when d1, d2 or d3 are equal to 0. So in when calculates alpha and beta (2 * d1 * d3) = 0 it returns NaN. Here is my fix:
|
@kalnitski The behavior you detailed is wrong. If d3 is 0, then if (d1 === 0 || d2 === 0) {
return 0;
} else if (d3 === 0) { // lineStart and lineEnd are the same - return point-to-point distance
return d1;
} else {
// math
} |
@cooperbarth Yeah, you are right, my mistake |
🎉 This issue has been resolved in version 3.2.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Hello @manuelbieh , I've just noticed that the fix you've commited and pushed does not deal with all the problematic cases. When we call This fix in if (d1 === 0 || d2 === 0) {
// point located at the exact same place as lineStart or lineEnd
return 0;
}
if (d3 === 0) {
return d1; // lineStart and lineEnd are the same - return point-to-point distance
} Tests I've created that still fail: it('https://github.com/manuelbieh/geolib/issues/227 - Point === startLine', () => {
expect(
getDistanceFromLine(
{
latitude: 53,
longitude: 5,
},
{
latitude: 53,
longitude: 5,
},
{
latitude: 53,
longitude: 6,
}
)
).toEqual(0);
});
// The test below does not fail but it's by accident.
it('https://github.com/manuelbieh/geolib/issues/227 - Point === endLine', () => {
expect(
getDistanceFromLine(
{
latitude: 53,
longitude: 5,
},
{
latitude: 53,
longitude: 6,
},
{
latitude: 53,
longitude: 5,
}
)
).toEqual(0);
});
it('https://github.com/manuelbieh/geolib/issues/227 - startLine === endLine', () => {
expect(
getDistanceFromLine(
{
latitude: 53,
longitude: 6,
},
{
latitude: 53,
longitude: 5,
},
{
latitude: 53,
longitude: 5,
}
)
).not.toBeNaN();
}); I don't have the access right to push this modification on a new branch on your repo. |
When I called the getDistanceFromLine method with the next variables, I'm getting a NaN result:
`const point = { longitude: -75.63287336843746, latitude: 6.278381350919607 };
const lineStart = {
longitude: -75.6220658304469,
latitude: 6.285304104233529 };
const lineEnd = {
longitude: -75.62216373107594,
latitude: 6.285232119894652 };
geolib.getDistanceFromLine(
point,
lineStart,
lineEnd
);
`
I don't know if it is related with the issue #129
The text was updated successfully, but these errors were encountered: