Skip to content

Instantly share code, notes, and snippets.

@isedgar
Last active December 9, 2024 18:14
Show Gist options
  • Save isedgar/d445248c9ff6c61cef44fc275cb2398f to your computer and use it in GitHub Desktop.
Save isedgar/d445248c9ff6c61cef44fc275cb2398f to your computer and use it in GitHub Desktop.
Naive pseudocode to create Voronoi cells (short version).

Voronoi diagram in the Cartesian plane

INPUT:

S := {(x0, y0), (x1, y1), ..., (xn-1, yn-1)}

pad := padding (some number)


ALGORITHM:

xl := minimum x-coordinate in S (left)
xr := maximum x-coordinate in S (right)
yt := maximum y-coordinate in S (top)
yb := minimum y-coordinate in S (bottom)

Box := {(xl-pad,yt+pad), (xr+pad,yt+pad), (xr+pad,yb-pad), (xl-pad,yb-pad)}

cells := {}

for each point p in S, do:
    cell := Box

    for each point q in S, except p, do:
        B = bisector(p, q)   *perpendicular bisector of p and q*

        if B intersects cell twice, then:
            Let t1 be the first intersection
            Let t2 be the second intersection
            Let xi be the vertex of cell after the first intersection
            Let xj be the vertex of cell after the second intersection

            new_cell := {}      *empty set*
            new_cell ∪ {t1}     *add first intersection*
            new_cell ∪ {xi}     *add vertex xi*
            new_cell ∪ {xi+1}   *add vertex xi+1*
            ⋮
            new_cell ∪ {t2}     *add second intersection*

            if p is not in new_cell, then:
                new_cell := {}
                new_cell ∪ {t2}     *add second intersection*
                new_cell ∪ {xj}     *add vertex xj*
                new_cell ∪ {xj+1}   *add vertex xj+1*
                ⋮
                new_cell ∪ {t1}     *add first intersection*

            cell := new_cell

    cells ∪ {cell}   *add cell to cells*

OUTPUT:

cells

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment