{"id":182,"date":"2026-11-13T12:33:48","date_gmt":"2026-11-13T04:33:48","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fortunes-algorithm-sweeps-a-voronoi-diagram\/"},"modified":"2026-11-13T21:51:58","modified_gmt":"2026-11-13T13:51:58","slug":"how-fortunes-algorithm-sweeps-a-voronoi-diagram","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fortunes-algorithm-sweeps-a-voronoi-diagram\/","title":{"rendered":"How Fortune&#8217;s algorithm sweeps a Voronoi diagram"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Fortune&#8217;s algorithm<\/strong> computes the Voronoi diagram of a planar point set by sweeping a horizontal line downward and maintaining a beach line of parabolic arcs. Each site above the sweep line owns the points closer to it than to the sweep line, and the boundary of those regions is a sequence of parabolas. Site events insert an arc. Circle events, where three arcs meet and the middle one disappears, record a Voronoi vertex. The sweep runs in <strong>O(n log n)<\/strong> time.<\/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\">The Voronoi cell of a site is the set of points closer to that site than to any other. A direct construction of every perpendicular bisector is quadratic. The sweep turns the global definition into a local one: above the sweep line the diagram is finished, and on the beach line the only arcs that can interact are neighbors. A new site splits one arc. A circle event removes an arc when the sweep reaches the bottom of a circle through three consecutive sites.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The priority queue holds site events, known in advance, and circle events, discovered when three consecutive arcs on the beach line have a circle whose lowest point is still below the sweep. A circle event is discarded if one of the arcs has already disappeared.<\/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\">The algorithm produces the Voronoi diagram, a planar graph with O(n) vertices and edges, in O(n log n) time. Extra memory is the beach line, usually a balanced tree of arcs, and the event queue. Degenerate cases, four cocircular sites or two sites on the same horizontal line, need a tie break or a symbolic perturbation. The Delaunay triangulation is the dual: connect sites whose cells share an edge. Computing the Delaunay triangulation and dualizing is an alternative route to the same diagram.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The beach line is not a polyline of bisectors already in the final diagram. The arcs move as the sweep moves. The stored output is the sequence of breakpoints and the vertices emitted at circle events.<\/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>Sort the sites by decreasing y. Insert them as site events in a priority queue. The beach line starts empty.<\/li>\n<li>On a site event, find the arc directly above the site on the current beach line, split it, and create two new breakpoints that trace a Voronoi edge. Check the new consecutive triples for circle events and queue any whose bottom lies below the sweep.<\/li>\n<li>On a circle event, record a Voronoi vertex at the circle&#8217;s center, delete the middle arc, and join the edges. Check the new neighboring triples for circle events. Drop the event if the arc is already gone.<\/li>\n<li>After the queue empties, attach the unfinished edges to a bounding box or leave them as infinite rays.<\/li>\n<li>The Delaunay triangulation connects the two sites that defined each finished Voronoi edge.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The n log n bound requires a logarithmic beach-line search. An array scan of the beach line at every event makes the algorithm quadratic. Cocircular degeneracies must be defined. A Delaunay triangulation computed another way is the dual, not a different geometric object.<\/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 a disappeared arc still fires its circle event<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Process every queued circle event even after an arc was deleted<\/td>\n<td>The triple no longer exists on the beach line. The event would insert a vertex that is not in the diagram. Test that the arc is still there.<\/td>\n<\/tr>\n<tr>\n<td>Scan the entire beach line as an array at every site<\/td>\n<td>The diagram can still be right and the time is O(n\u00b2). The beach line has to be a search tree keyed by x.<\/td>\n<\/tr>\n<tr>\n<td>Ignore two sites with the same y<\/td>\n<td>The sweep hits them together. Without a tie break the arc insertion order is undefined and edges can cross.<\/td>\n<\/tr>\n<tr>\n<td>Expect the beach-line parabolas to be stored as the output<\/td>\n<td>They are the sweep state. The output is the vertices and edges left behind.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">The dual graph is the Delaunay triangulation. A convex hull is the limit of a Voronoi diagram with a point at infinity, and it is computed directly by Andrew or Graham. Fortune&#8217;s algorithm is the beach-line sweep.<\/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 sites from top to bottom, splits the beach arc above each site, and closes an arc when the sweep hits the bottom of a circle event. The three sites (0, 0), (2, 0), and (1, 2) produce one Voronoi vertex at <code>1.00 0.75<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import heapq\nsites = [(0.0, 0.0), (2.0, 0.0), (1.0, 2.0)]\n\nclass Arc:\n    def __init__(self, site):\n        self.site = site\n        self.event = None\n\ndef cross(a, b, c):\n    return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])\n\ndef circle(a, b, c):\n    d = 2 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]))\n    if abs(d) &lt; 1e-12:\n        return None\n    ux = ((a[0] ** 2 + a[1] ** 2) * (b[1] - c[1]) + (b[0] ** 2 + b[1] ** 2) * (c[1] - a[1]) + (c[0] ** 2 + c[1] ** 2) * (a[1] - b[1])) \/ d\n    uy = ((a[0] ** 2 + a[1] ** 2) * (c[0] - b[0]) + (b[0] ** 2 + b[1] ** 2) * (a[0] - c[0]) + (c[0] ** 2 + c[1] ** 2) * (b[0] - a[0])) \/ d\n    r = ((ux - a[0]) ** 2 + (uy - a[1]) ** 2) ** 0.5\n    return (ux, uy), r\n\ndef intersections(p, q, ly):\n    px, py = p\n    qx, qy = q\n    dp, dq = py - ly, qy - ly\n    if abs(dp) &lt; 1e-12:\n        return [px]\n    if abs(dq) &lt; 1e-12:\n        return [qx]\n    a = dq - dp\n    b = -2 * px * dq + 2 * qx * dp\n    c = dq * px * px - dp * qx * qx + dp * dq * (py - qy)\n    if abs(a) &lt; 1e-12:\n        return [] if abs(b) &lt; 1e-12 else [-c \/ b]\n    disc = b * b - 4 * a * c\n    if disc &lt; 0:\n        return []\n    root = disc ** 0.5\n    return [(-b - root) \/ (2 * a), (-b + root) \/ (2 * a)]\n\ndef breakpoint(left, right, ly):\n    xs = intersections(left, right, ly)\n    if not xs:\n        return (left[0] + right[0]) \/ 2\n    def on_beach(focus, x):\n        return (x - focus[0]) ** 2 \/ (2 * (focus[1] - ly)) + (focus[1] + ly) \/ 2\n    chosen = xs[0]\n    for x in xs:\n        if abs(left[1] - ly) &lt; 1e-12 or abs(right[1] - ly) &lt; 1e-12:\n            return x\n        if on_beach(left, x - 1e-4) &lt;= on_beach(right, x - 1e-4):\n            chosen = x\n    return chosen\n\narcs = []\nevents = []\nstamp = 0\nvertices = []\nfor site in sites:\n    stamp += 1\n    heapq.heappush(events, (-site[1], site[0], stamp, &quot;site&quot;, site))\n\ndef arc_at(x, ly):\n    for i in range(len(arcs) - 1):\n        if x &lt; breakpoint(arcs[i].site, arcs[i + 1].site, ly):\n            return i\n    return len(arcs) - 1\n\ndef consider(i, ly):\n    global stamp\n    if i &lt;= 0 or i &gt;= len(arcs) - 1:\n        return\n    a, b, c = arcs[i - 1], arcs[i], arcs[i + 1]\n    if cross(a.site, b.site, c.site) &gt;= 0:\n        return\n    got = circle(a.site, b.site, c.site)\n    if got is None:\n        return\n    center, radius = got\n    bottom = center[1] - radius\n    if bottom &gt; ly + 1e-8:\n        return\n    stamp += 1\n    b.event = stamp\n    heapq.heappush(events, (-bottom, center[0], stamp, &quot;circle&quot;, (b, center)))\n\nwhile events:\n    neg_y, _, eid, kind, payload = heapq.heappop(events)\n    ly = -neg_y\n    if kind == &quot;site&quot;:\n        site = payload\n        if not arcs:\n            arcs.append(Arc(site))\n            continue\n        i = arc_at(site[0], ly - 1e-8)\n        arcs[i].event = None\n        arcs[i:i + 1] = [Arc(arcs[i].site), Arc(site), Arc(arcs[i].site)]\n        consider(i, ly)\n        consider(i + 1, ly)\n        consider(i + 2, ly)\n    else:\n        arc, center = payload\n        if arc.event != eid or arc not in arcs:\n            continue\n        i = arcs.index(arc)\n        vertices.append(center)\n        arcs[i - 1].event = None\n        arcs[i + 1].event = None\n        del arcs[i]\n        consider(i - 1, ly)\n        consider(i, ly)\n\nassert len(vertices) == 1\nvx, vy = vertices[0]\nassert abs(vx - 1) &lt; 1e-6 and abs(vy - 0.75) &lt; 1e-6\nprint(f&quot;{vx:.2f}&quot;, f&quot;{vy:.2f}&quot;)\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\">Fortune&#8217;s sweep builds a Voronoi diagram, the nearest-region map used in GIS and in facility location. Computational-geometry libraries also compute the same diagram from a convex hull in one higher dimension. A k-d tree answers nearest neighbor queries without drawing the cells.<\/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 a quadratic bisector loop for any large point set. Use the sweep, with a real search tree for the beach line and a cancellation test on circle events.<\/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 Fortune&#8217;s algorithm returns the beach line, the site and circle events, and the O(n log n) bound. <strong>Fortune&#8217;s algorithm computes a Voronoi diagram in O(n log n) time by sweeping a beach line of parabolic arcs and emitting a vertex at each valid circle event.<\/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: Fortune&#8217;s algorithm computes the Voronoi diagram of a planar point set by sweeping a horizontal line downward and maintaining a beach line of parabolic arcs. Each site above the sweep line owns the points closer to it than to the sweep line, and the boundary of those&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-182","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\/182","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=182"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/182\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}