fix(@turf/polygon-tangents): return distinct tangents when nearest vertex is below viewpoint inside bbox#3083
Open
JSap0914 wants to merge 3 commits into
Conversation
When start and end points are identical, rhumbBearing() was returning 0 (north) because Math.atan2(0, 0) === 0. This is misleading since no bearing can be defined for a zero-length segment. The Geodesy library (which this implementation is adapted from) explicitly guards for this case and returns NaN. Align turf's behaviour accordingly. Closes Turfjs#2478
…rtex is below viewpoint inside bbox When the viewpoint lies inside the polygon bounding box and the nearest polygon vertex is below the viewpoint, the existing code initialises both rtan and ltan to that same nearest vertex. processPolygon then cannot update ltan because every above-viewpoint candidate compares as 'above' the current ltan (which is already below the viewpoint), so the function returns the same point for both tangents. Detect this degenerate outcome (rtan === ltan after processPolygon) and retry with ltan reset to vertex[0], which lets the traversal find the correct left-tangent vertex. Fixes Turfjs#2898
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
polygonTangentsreturns the same point for both tangents when the viewpoint lies inside the polygon bounding box and the nearest polygon vertex is below the viewpoint.Root cause. When the point is inside the bbox, the existing code initialises both
rtanandltanto the nearest vertex (line 63–65 ofindex.ts). When that nearest vertex is below the viewpoint,processPolygoncannot updateltanto any candidate above the viewpoint, because every such candidate is consideredisAbovethe currentltanand therefore rejected. The traversal ends withrtan === ltan.Fix. After
processPolygonreturns, detect the degenerate case (rtan === ltanandnearestPtIndex !== 0) and retry withltanreset topolyCoords[0][0]. This allows the second traversal to find the correct left-tangent vertex while preserving the existing behaviour for all other cases (including the concave-edge fix introduced for issues #1050, #785, and #1032).Resolves
Closes #2898
Verification
TAP version 13
turf-polygon-tangents
ok 1 concave
ok 2 high
ok 3 issue#1032
ok 4 issue#1050
ok 5 issue#2898
ok 6 issue#785
ok 7 multipolygon
ok 8 polygonWithHole
ok 9 square
turf-polygon-tangents - Geometry Objects
ok 10 should be truthy
turf-polygon-tangents - Prevent Input Mutation
ok 11 pt should not mutate
ok 12 poly should not mutate
turf-polygon-tangents - Issue #1050
ok 13 should be truthy
turf-polygon-tangents - Issue #2898
ok 14 returns two features
ok 15 tangent points must be distinct vertices
1..15
tests 15
pass 15
ok
All 12 pre-existing tests still pass. The 3 new tests (1 fixture snapshot + 2 inline assertions) go RED on the original code and GREEN after this fix.