Robust 2D Segment Intersection With Floating Point Tolerance
Last updated: November 29, 2026
Quick Overview
Write a function that decides whether two 2D line segments intersect, including touching endpoints and collinear overlap, and make it behave sensibly when coordinates come from floating point CAD data.
Autodesk
November 29, 20260
0
0 solved
Write a function that decides whether two 2D line segments intersect, including touching endpoints and collinear overlap, and make it behave sensibly when coordinates come from floating point CAD data.
Geometry kernels behind AutoCAD trim and extend, Fusion sketches, and Revit wall joins all depend on predicates like this, and naive equality checks cause the hardest bugs. The interviewer probes orientation tests, the special cases, and how the candidate thinks about tolerance and scale.
What the Interviewer Expects
- Use the orientation (cross product) predicate instead of solving for slopes
- Handle the general proper crossing case
- Handle collinear and touching cases with an on-segment check
- Use a tolerance and explain why it must scale with coordinate magnitude
- Discuss exact or adaptive predicates as the robust alternative
- Mention consistency: the same predicate must give the same answer everywhere
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Why is a fixed epsilon like 1e-9 wrong for a site plan in millimeters with coordinates near 1e7?
- How do exact or adaptive precision predicates avoid tolerance altogether?
- How would you compute the intersection point, and what do you return for collinear overlap?
- Why can tolerance-based predicates break transitivity, and where does that hurt?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Return True if segments p1p2 and q1q2 share any point, including endpoints touching and collinear overlap. Inputs are floats from a drawing, so results that should be exactly zero (a T-junction drawn ...
High-Level Design
orient(a, b, c) gives the sign of the cross product: which side of line ab the point c is on. Two segments properly cross when each one's endpoints lie on opposite sides of the other. If some orient...