Line Segment Intersection
The Two-Segment Problem
Input: Two line segments and (four endpoints, given as coordinates).
Output: true if they intersect, false otherwise.
The Straddle Test
Two segments intersect if and only if each segment straddles the line containing the other — or an endpoint of one lies on the other segment.
For segment to straddle the line through , the endpoints and must lie on opposite sides of the infinite line through (or one endpoint lies exactly on that line).
Define the direction of with respect to segment :
This is the orientation test with as origin.
Algorithm
SEGMENTS-INTERSECT(p1, p2, p3, p4):
d1 = DIRECTION(p3, p4, p1)
d2 = DIRECTION(p3, p4, p2)
d3 = DIRECTION(p1, p2, p3)
d4 = DIRECTION(p1, p2, p4)
// Both segments straddle each other's line
if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0)) and
((d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)):
return true
// Check collinear cases: endpoint lies on the other segment
if d1 == 0 and ON-SEGMENT(p3, p4, p1): return true
if d2 == 0 and ON-SEGMENT(p3, p4, p2): return true
if d3 == 0 and ON-SEGMENT(p1, p2, p3): return true
if d4 == 0 and ON-SEGMENT(p1, p2, p4): return true
return false
ON-SEGMENT(pi, pj, pk):
return (min(xi, xj) <= xk <= max(xi, xj)) and
(min(yi, yj) <= yk <= max(yi, yj))
ON-SEGMENT checks that the collinear point actually falls within the segment bounds (not just on the infinite line).
Worked Example
Segments: , , , .
Compute the four directions:
and , so straddles the line of . Check the other pair:
and , so straddles the line of . Both conditions met → intersection at . Correct.
The n-Segment Intersection Problem
Input: line segments.
Output: true if any pair intersects; false otherwise.
Naive:
Check all pairs. Impractical for large .
Sweep-Line Algorithm:
Where is the number of intersections reported.
The idea: sweep a vertical line from left to right across the plane. Maintain two data structures:
- Event queue (priority queue): endpoints sorted by -coordinate, plus intersection points discovered during the sweep.
- Sweep-line status (balanced BST): segments currently intersecting the sweep line, ordered by their -coordinate at the sweep line’s position.
When the sweep line reaches:
- Left endpoint: insert segment into the BST; check the segment above and below for intersection.
- Right endpoint: remove segment from the BST; check the newly adjacent segments for intersection.
- Intersection point: swap the two intersecting segments in the BST and check their new neighbours.
Key insight: segments can only intersect if they become adjacent in the sweep-line ordering at some point. The BST ensures we only check pairs rather than .
Special Cases
- Vertical segments: handled by the event queue ordering.
- Multiple segments sharing an endpoint: careful tie-breaking in the comparison.
- Collinear overlapping segments: detect and merge or report.
Summary
| Problem | Algorithm | Time |
|---|---|---|
| Two segments | Straddle test (4 cross products) | |
| segments (any pair) | Naive all-pairs | |
| segments (any pair) | Sweep-line | |
| Cross product use | Orientation test, no division | Exact for integer coords |
Exam Note: You may be asked to trace the straddle test on a specific pair of segments, or to explain the sweep-line approach. Tripos questions on geometry are new for 25/26; expect ~bookwork plus small tracing.