Skip to content

Intersections: treat vertical segments correctly in ToleranceEdgeY - #393

Closed
anaelorlinski wants to merge 1 commit into
tdewolff:masterfrom
anaelorlinski:fix/bo-tolerance-square-boundary
Closed

anaelorlinski wants to merge 1 commit into
tdewolff:masterfrom
anaelorlinski:fix/bo-tolerance-square-boundary

Conversation

@anaelorlinski

Copy link
Copy Markdown
Contributor

I asked claude to find the root of a bug I had in the past and workarounded with perturbation at clipping edges.
Bug has been reproduced and here is the fix.

ToleranceEdgeY interpolates a segment's y at the left and right edges of a
tolerance square. For a vertical segment the x-span is zero, so whenever
floating-point rounding puts the segment's raw x one ulp outside
[x - eps/2, x + eps/2) the interpolation divides by zero and returns NaN.
NaN fails every below/above comparison in breakupCrossingSegments, so the
segment is never broken up at the squares it passes through in its column.

That happens in practice when a shape is intersected with a clip whose edge
nearly coincides with one of its own edges (within BentleyOttmannEpsilon):
the crossing snaps to one grid point and the endpoint to the next one up,
the clip's vertical edge should be split there, is not, and the result
polygon walk finds no continuation. Outside debug mode the contour is
closed early and a wedge of the shape goes missing; in debug mode it panics
with "next node for result polygon is nil".

A vertical segment has both endpoints in the column, so its y-values at the
tolerance edges are just its endpoints. Return them directly.

The regression test intersects a rounded rectangle with a same-size
rectangle offset by 2e-9, with the shared edge on a snap half-grid line:
the result had two subpaths and 35% of the area missing. The randomized
variant fails about 5% of 1000 cases before this change and none after.

This does not fix the dense merged-grid case from tdewolff#382, which fails at a
vertex holding three identical overlapping segments and has a different
cause.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@anaelorlinski
anaelorlinski deleted the fix/bo-tolerance-square-boundary branch September 5, 2026 20:58
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