Line Sweep Algorithm Simulator: Segment Intersection Detection

simulator intermediate ~10 min
Loading simulation...
~12 intersections from 15 segments

15 random segments typically produce about 12 intersections. The sweep-line algorithm finds them in ~42 events versus 105 brute-force comparisons — a 2.5x speedup that grows with scale.

Formula

Bentley-Ottmann: O((n + k) log n) time, O(n + k) space
Naive: O(n^2) comparisons = n(n-1)/2
Events: 2n endpoints + k intersection points

The Sweep Line Paradigm

Sweep line algorithms transform two-dimensional geometric problems into sequences of one-dimensional events. An imaginary vertical line sweeps from left to right across the plane, maintaining a status structure of geometric objects currently intersecting it. Whenever the line hits an event point — a segment endpoint or intersection — the status updates and the algorithm responds. This paradigm powers many of computational geometry's most elegant solutions.

Bentley-Ottmann Algorithm

Given n line segments, finding all k intersections naively requires checking all O(n²) pairs. Bentley and Ottmann's 1979 algorithm does better: it only checks segments that are adjacent in the sweep line's status structure. When two segments become adjacent (due to an insertion or a swap), it tests for intersection and schedules the result as a future event. Total time: O((n + k) log n).

The Event Queue and Status Tree

Two data structures drive the algorithm. The event queue (a priority queue) stores segment endpoints and discovered intersections, ordered by x-coordinate. The status tree (a balanced BST) maintains the vertical order of segments crossing the current sweep line position. This simulator visualizes both structures: the sweep line moves across the canvas while the status tree reorders segments in real time.

Beyond Segments

The sweep line paradigm extends far beyond segment intersection. Fortune used it to compute Voronoi diagrams in O(n log n). The same idea solves rectangle union area, closest pair of points, and Boolean operations on polygons. Understanding the sweep line means understanding a universal technique that appears throughout computational geometry and geographic information systems.

FAQ

What is a sweep line algorithm?

A sweep line algorithm solves geometric problems by moving an imaginary vertical line across the plane from left to right. It maintains a dynamic status structure of objects currently intersecting the sweep line. Events (segment endpoints and intersections) are processed in order, reducing a 2D problem to a sequence of 1D updates.

What is the Bentley-Ottmann algorithm?

The Bentley-Ottmann algorithm (1979) finds all k intersections among n line segments in O((n + k) log n) time. It uses a sweep line with an event queue (priority queue) and status structure (balanced BST). When adjacent segments in the status structure swap order, an intersection is detected.

Why not just check all pairs?

Checking all n(n-1)/2 pairs takes O(n²) time regardless of how many intersections exist. The sweep line algorithm is output-sensitive: it runs in O((n + k) log n), which is much faster when k << n². For n = 1000 segments with 100 intersections, sweep line is ~100x faster.

Where are sweep line algorithms used?

Sweep line algorithms solve: segment intersection detection, Voronoi diagram construction (Fortune's algorithm), Boolean operations on polygons, closest pair of points, and rectangle union area. The paradigm is one of computational geometry's most powerful general techniques.

Sources

Embed

<iframe src="https://homo-deus.com/lab/computational-geometry/line-sweep/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub