The trick is to never ask "do these two segments cross?" for a pair that is far apart. Bentley-Ottmann keeps two structures moving together:
- The event queue. A priority queue of x-coordinates where something interesting can happen: a segment starts, a segment ends, or two segments cross. It begins with just the 2n endpoints and grows as new crossing events are discovered.
- The status structure. A balanced order (conceptually a sorted list, in practice a balanced binary search tree) of the segments currently crossed by the sweep line, ordered by their y-coordinate at the line's current position. Only segments that are adjacent in this order are ever tested for intersection.
At each event the sweep does a constant amount of structural work — insert a segment, delete a segment, or swap two neighbors — plus O(logn) to keep the status structure balanced. Insertions and deletions happen at most 2n times (once per endpoint); crossings happen exactly K times, the number of intersection points. So the total number of events is O(n+K), and each costs O(logn):
T(n,K)=O((n+K)logn)
This is an output-sensitive bound: the running time scales with how many intersections actually exist, not just with n. In the worst case K can be O(n2) (every pair crosses), and the algorithm gracefully degrades to roughly the brute-force bound — but whenever K is small, which is the common case for real maps and drawings, Bentley-Ottmann is dramatically faster than checking every pair. Space stays O(n) for the status and queue, ignoring the room needed to output the crossings themselves. This same insert-delete-swap idea is a workhorse of the broader computational geometry toolbox.
Comments
Loading comments...