Skip to content

fix(boolean-crosses): a LineString that only touches a Polygon boundary does not cross it#3091

Open
spokodev wants to merge 2 commits into
Turfjs:masterfrom
spokodev:w33/turf-boolean-crosses-touch
Open

fix(boolean-crosses): a LineString that only touches a Polygon boundary does not cross it#3091
spokodev wants to merge 2 commits into
Turfjs:masterfrom
spokodev:w33/turf-boolean-crosses-touch

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

booleanCrosses returned true for a LineString that only touches a Polygon
boundary without passing through the interior.

doLineStringAndPolygonCross treated any intersection between the line and the
polygon boundary as a crossing. Per the OGC definition a cross requires the line
to have parts both inside and outside the polygon. A line that merely touches the
boundary at a point (endpoint on a vertex or edge) has all of its interior on one
side, so it does not cross.

Concrete cases on the polygon [[0,0],[0,10],[10,10],[10,0],[0,0]]:

  • Line [[0,5],[5,5]] lies inside the polygon and touches the left edge. It is
    booleanWithin === true, yet the old code also returned booleanCrosses === true,
    which is contradictory.
  • Line [[-5,-5],[0,0],[-5,5]] lies outside and touches a corner. The old code
    returned true.

Both match the JSTS Geometry.crosses reference, which returns false for them.

The fix splits the line on the polygon boundary and checks the midpoint of each
resulting sub-segment. A cross is reported only when at least one sub-segment lies
strictly inside and another lies strictly outside. Genuine crossings still return
true.

Added a false fixture for the inside-touching case.

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.

1 participant