Skip to content

turf-nearest-point-on-line: fix for points far away from the segment endpoints #2871

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mlc
Copy link

@mlc mlc commented Apr 22, 2025

Fixes #2870; see that ticket for a description of the observed bug.

This patch fixes the described bug by adjusting how nearestPointOnSegment picks an endpoint when it determines that the nearest intersection of the great circles is not on the segment. Rather than doing work with angles, it just computes the distance to the two endpoints and picks the closer one.

I think this is safe—it does not break any existing tests—and should not lead to a performance reduction (there are just as many calls to distance as there were before, and some vector math has been removed), but the algorithms here are a little bit outside of my wheelhouse, so I would certainly appreciate any feedback if there is something wrong. A more natural way of fixing the issue might be to correctly choose the right value of I in the section above the one I modified, but I can not quite figure out how to make that work.

Thanks so much for turf and for your attention to this!

Please provide the following when creating a PR:

  • Meaningful title, including the name of the package being modified.
  • Summary of the changes.
  • Heads up if this is a breaking change.
  • Any issues this resolves.
  • Inclusion of your details in the contributors field of package.json - you've earned it! 👏
  • Confirmation you've read the steps for preparing a pull request.

mlc added a commit to mlc/tpg-solver that referenced this pull request Apr 22, 2025
take Turfjs/turf#2871 early while we see if it's right
@mlc mlc changed the title fix nearestPointOnLine for points far away from the segment endpoints turf-nearest-point-on-line: fix for points far away from the segment endpoints Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nearestPointOnLine can return wrong endpoint when the target point is very far from the line
1 participant