How Bentley-Ottmann sweeps segment intersections

Short answer: The Bentley-Ottmann algorithm reports every intersection of a set of line segments by sweeping a vertical line from left to right. A balanced tree holds the segments that cross the sweep line, ordered by y. A priority queue holds left endpoints, right endpoints, and future intersection points of segments that are currently neighbors. Only neighbors can intersect before one of them meets someone else, so the sweep examines O((n + k) log n) events for n segments and k intersections.


What the problem actually asks

Testing every pair of segments is quadratic and finds every intersection, including the ones that do not exist only after a full test. A sweep notices an intersection at the moment the two segments become adjacent in the y-order and exchanges them. Intersections of segments that are never adjacent along the sweep are impossible without a third event in between, which the queue will see first.

The argument needs general position or a careful tie break: no two endpoints with the same x, no three segments meeting at one point, or an explicit symbolic perturbation. A naive implementation that ignores ties reports duplicate events or drops an intersection.


What the algorithm guarantees

Every proper intersection is reported once, at its x coordinate, under a correct event policy. Time is O((n + k) log n) with balanced trees. Extra memory is O(n + k) if intersection events are stored, or less if they are discarded after reporting. The algorithm reports crossings. It does not compute a polygon arrangement with faces unless the sweep also stitches half-edges. That extension is a planar map overlay and uses the same event queue.

Horizontal segments have a constant y on the sweep and need a defined order against the line. Vertical segments break a vertical sweep. A standard fix is a symbolic shear or a separate case. Ignoring vertical segments drops their intersections.


The working steps

  1. Insert every left endpoint and every right endpoint into a priority queue ordered by x, with a tie break on event type and y.
  2. The sweep status is a balanced tree of segments currently crossed by the sweep line, ordered by the y of that crossing.
  3. On a left endpoint, insert the segment and test it against its new neighbors. If a neighbor pair intersects to the right of the sweep, insert that intersection event.
  4. On a right endpoint, remove the segment and test the neighbors that just became adjacent.
  5. On an intersection event, swap the two segments in the status tree, report the point, and test the new neighbor pairs. Discard an intersection event whose segments are no longer neighbors. It was superseded.

The (n+k) log n bound counts events at endpoints and intersections. It is not a license to skip the neighbor-only test and still quote the bound. All-pairs testing is simpler and quadratic. A vertical sweep that does not define vertical segments is incomplete. Robust predicates matter: an orientation test that flips under rounding inserts a crossing on the wrong side of the sweep and the queue order collapses.


Approaches that look simpler and fail

Approach Why it fails when non-adjacent segments are tested as a substitute for the status tree
Test every pair and skip the sweep The intersections are found in Θ(n²) tests. The sweep exists to test only adjacent pairs and future events.
Leave a stale intersection in the queue after a swap The two segments may no longer meet before another event. Processing a stale event swaps the wrong pair. Check that they are still neighbors.
Use a vertical sweep on vertical segments without a special case A vertical segment is not crossed at a single y in a well-defined way by a vertical line that contains it. Shear the coordinates or handle the case.
Quote O(n log n) when many segments cross The bound includes k, the number of intersections. A complete crossing set has k near n² and the algorithm is no longer near-linear.

A convex hull does not report interior crossings. A polygon overlay that needs faces, not only points, extends this sweep. Bentley-Ottmann is the sweep that reports segment intersections.


A working example

This Python fragment sweeps endpoints and crossings from left to right. The status stays ordered by the y where the sweep meets each segment, and only current neighbors are tested. The segments (0, 0)–(2, 2) and (0, 2)–(2, 0) cross at 1 1.

import heapq
segments = [((0, 0), (2, 2)), ((0, 2), (2, 0))]

def y_at(seg, x):
    (x1, y1), (x2, y2) = seg
    t = (x - x1) / (x2 - x1)
    return y1 + t * (y2 - y1)

def crossing(i, j):
    (x1, y1), (x2, y2) = segments[i]
    (x3, y3), (x4, y4) = segments[j]
    den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
    if den == 0:
        return None
    t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den
    u = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) / den
    if 0 < t < 1 and 0 < u < 1:
        return (x1 + t * (x2 - x1), y1 + t * (y2 - y1))
    return None

events = []
for i, seg in enumerate(segments):
    left, right = sorted(seg)
    heapq.heappush(events, (left[0], 0, left[1], i))
    heapq.heappush(events, (right[0], 2, right[1], i))
status, found, queued = [], [], set()

def consider(i, j, sweep_x):
    if i is None or j is None:
        return
    point = crossing(i, j)
    if point is None or point[0] <= sweep_x + 1e-9:
        return
    key = (round(point[0], 6), round(point[1], 6))
    if key not in queued:
        queued.add(key)
        heapq.heappush(events, (point[0], 1, point[1], (i, j)))

while events:
    x, kind, y, data = heapq.heappop(events)
    if kind == 0:
        status.append(data)
        status.sort(key=lambda s: y_at(segments[s], x + 1e-9))
        pos = status.index(data)
        above = status[pos + 1] if pos + 1 < len(status) else None
        below = status[pos - 1] if pos else None
        consider(data, above, x)
        consider(data, below, x)
    elif kind == 2:
        pos = status.index(data)
        above = status[pos + 1] if pos + 1 < len(status) else None
        below = status[pos - 1] if pos else None
        status.pop(pos)
        consider(below, above, x)
    else:
        i, j = data
        if i not in status or j not in status:
            continue
        a, b = status.index(i), status.index(j)
        if abs(a - b) != 1:
            continue
        found.append((round(x), round(y)))
        status[a], status[b] = status[b], status[a]
assert found == [(1, 1)]
print(*found[0])

Where it is used

Bentley-Ottmann is the sweep that reports segment intersections in CAD, GIS overlays, and circuit layouts. Geometry libraries such as CGAL use a sweep for this problem. Checking every pair is correct and quadratic.


When you can drop this algorithm

Drop the sweep when n is tiny and an all-pairs test is easier to verify. Keep it when k is expected to be much smaller than n², and define the tie and vertical-segment policy before writing the queue.


Why this stays published

We publish this so a search for Bentley-Ottmann returns the sweep status of neighboring segments and the O((n+k) log n) event bound. Bentley-Ottmann reports segment intersections by sweeping left to right and testing only segments that are neighbors on the sweep line, in O((n+k) log n) time.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *