{"id":140,"date":"2026-11-13T12:32:31","date_gmt":"2026-11-13T04:32:31","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bentley-ottmann-sweeps-segment-intersections\/"},"modified":"2026-11-13T21:51:09","modified_gmt":"2026-11-13T13:51:09","slug":"how-bentley-ottmann-sweeps-segment-intersections","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bentley-ottmann-sweeps-segment-intersections\/","title":{"rendered":"How Bentley-Ottmann sweeps segment intersections"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Bentley-Ottmann<\/strong> 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 <strong>O((n + k) log n)<\/strong> events for n segments and k intersections.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Insert every left endpoint and every right endpoint into a priority queue ordered by x, with a tie break on event type and y.<\/li>\n<li>The sweep status is a balanced tree of segments currently crossed by the sweep line, ordered by the y of that crossing.<\/li>\n<li>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.<\/li>\n<li>On a right endpoint, remove the segment and test the neighbors that just became adjacent.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when non-adjacent segments are tested as a substitute for the status tree<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Test every pair and skip the sweep<\/td>\n<td>The intersections are found in \u0398(n\u00b2) tests. The sweep exists to test only adjacent pairs and future events.<\/td>\n<\/tr>\n<tr>\n<td>Leave a stale intersection in the queue after a swap<\/td>\n<td>The two segments may no longer meet before another event. Processing a stale event swaps the wrong pair. Check that they are still neighbors.<\/td>\n<\/tr>\n<tr>\n<td>Use a vertical sweep on vertical segments without a special case<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(n log n) when many segments cross<\/td>\n<td>The bound includes k, the number of intersections. A complete crossing set has k near n\u00b2 and the algorithm is no longer near-linear.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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)\u2013(2, 2) and (0, 2)\u2013(2, 0) cross at <code>1 1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import heapq\nsegments = [((0, 0), (2, 2)), ((0, 2), (2, 0))]\n\ndef y_at(seg, x):\n    (x1, y1), (x2, y2) = seg\n    t = (x - x1) \/ (x2 - x1)\n    return y1 + t * (y2 - y1)\n\ndef crossing(i, j):\n    (x1, y1), (x2, y2) = segments[i]\n    (x3, y3), (x4, y4) = segments[j]\n    den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)\n    if den == 0:\n        return None\n    t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) \/ den\n    u = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) \/ den\n    if 0 &lt; t &lt; 1 and 0 &lt; u &lt; 1:\n        return (x1 + t * (x2 - x1), y1 + t * (y2 - y1))\n    return None\n\nevents = []\nfor i, seg in enumerate(segments):\n    left, right = sorted(seg)\n    heapq.heappush(events, (left[0], 0, left[1], i))\n    heapq.heappush(events, (right[0], 2, right[1], i))\nstatus, found, queued = [], [], set()\n\ndef consider(i, j, sweep_x):\n    if i is None or j is None:\n        return\n    point = crossing(i, j)\n    if point is None or point[0] &lt;= sweep_x + 1e-9:\n        return\n    key = (round(point[0], 6), round(point[1], 6))\n    if key not in queued:\n        queued.add(key)\n        heapq.heappush(events, (point[0], 1, point[1], (i, j)))\n\nwhile events:\n    x, kind, y, data = heapq.heappop(events)\n    if kind == 0:\n        status.append(data)\n        status.sort(key=lambda s: y_at(segments[s], x + 1e-9))\n        pos = status.index(data)\n        above = status[pos + 1] if pos + 1 &lt; len(status) else None\n        below = status[pos - 1] if pos else None\n        consider(data, above, x)\n        consider(data, below, x)\n    elif kind == 2:\n        pos = status.index(data)\n        above = status[pos + 1] if pos + 1 &lt; len(status) else None\n        below = status[pos - 1] if pos else None\n        status.pop(pos)\n        consider(below, above, x)\n    else:\n        i, j = data\n        if i not in status or j not in status:\n            continue\n        a, b = status.index(i), status.index(j)\n        if abs(a - b) != 1:\n            continue\n        found.append((round(x), round(y)))\n        status[a], status[b] = status[b], status[a]\nassert found == [(1, 1)]\nprint(*found[0])\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Bentley-Ottmann is the sweep that reports segment intersections in CAD, GIS overlays, and circuit layouts. Geometry libraries such as <strong>CGAL<\/strong> use a sweep for this problem. Checking every pair is correct and quadratic.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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\u00b2, and define the tie and vertical-segment policy before writing the queue.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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. <strong>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.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,40],"tags":[],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-geometry"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}