Voronoi diagrams: who is closest?
Place a few sites on a plane. Give every location to its nearest site, and a mosaic of convex cells appears. Move just one site and watch the boundaries change.
Video walkthrough
A map of nearest neighbours
Imagine choosing the closest delivery hub by straight-line distance. A site is a hub, and its Voronoi cell contains all locations at least as close to it as to every other hub. On a shared boundary, two or more sites tie; the boundary belongs to each of their closed cells.
This geometric model is useful for spatial queries and procedural textures. It does not account for roads, obstacles or capacity: the nearest site in Euclidean distance need not be the quickest destination to reach.
Two sites give us one straight boundary
Try the Two sites preset: A=(25,35) and B=(75,35). At (50,35), both distances are 25. Every point with x=50 is equidistant from A and B. To its left A is closer; to its right B is closer. The boundary is the perpendicular bisector of the segment AB.
For a query p and sites a, b, compare squared distances: |p−a|² ≤ |p−b|². After cancelling the quadratic terms, the test becomes (p−(a+b)/2)·(b−a) ≤ 0. This linear inequality defines the half-plane containing a. Square roots are needed only for the distances displayed in the inspector.
Build a cell by cutting away losing territory
Start a site’s candidate cell as the whole 100 × 70 rectangle. Compare it with each other site: draw their perpendicular bisector, then clip the candidate polygon to the side closer to the active site. Repeat for every site. The dashed line shows the current comparison; a coloured circle with a thick outline marks the active site.
The invariant is simple: after k comparisons, the candidate contains exactly the locations inside the rectangle where the active site is no farther away than those k rivals. Another intersection preserves that property. After all rivals, it is the clipped Voronoi cell. Intersections of convex half-planes stay convex.
Completed cells remain on screen while the next candidate is constructed. A candidate may temporarily overlap them because it has not passed all comparisons. Those overlaps disappear in the result. Polygon boundaries are drawn directly, so this is a vector construction rather than colouring a raster grid.
Three experiments
Jump to the result and drag a site. The updated cells stay visible when you release it. Adding, removing or moving sites in the editor also shows the completed diagram. Use Restart to inspect the new construction step by step. Choose Add site for clicks on empty space, or enter coordinates in the editor. Removing sites can make their neighbours’ cells expand.
Choose On one line. Parallel bisectors split the rectangle into strips. With a single site, its cell is the entire rectangle. These cases need no special geometric workaround.
Test a point with the cross or coordinate fields. The distance list always uses all sites, including during unfinished construction. Thick lines connect the query to every nearest site. For the two-site preset, use (50,35) to see an exact tie. Press Start, pause, move backwards or use the step slider to inspect each cut.
The window, boundaries and numerical precision
Some Voronoi cells extend to infinity. Here they are intersected with the visible rectangle. Its outer border is a display boundary, not an equal-distance boundary between sites. Coordinates run rightward from x=0 to 100 and downward from y=0 to 70.
The editor accepts 1–12 distinct sites, including points on the frame, and rounds coordinates to 0.1. Coincident sites are rejected because they would have the same distance everywhere. Collinear sites and ties between three or more sites are supported. Ties in the distance inspector use an absolute tolerance of 10⁻⁸ coordinate units.
Clipping uses JavaScript floating-point arithmetic. Consecutive vertices less than 10⁻⁸ apart are merged; displayed distances are rounded to two decimals, so equal-looking numbers are not necessarily a tie. This small educational model does not provide exact predicates for arbitrary-scale geometry.
What the counters measure
Cuts counts one polygon/half-plane intersection; even a cut that removes nothing counts. For n sites there are n(n−1) cuts. Polygon edges tested sums the number of candidate sides examined by these cuts. Completed cells counts fully processed sites.
A candidate has at most O(n) sides. This straightforward implementation therefore takes O(n³) time in the worst case: n cells, n−1 clips per cell, and up to O(n) work per clip. The saved playback history shares immutable polygons and copies arrays of cell references, using at most O(n³) memory. These are conservative bounds for this implementation, not the optimal bounds for Voronoi diagrams.
A planar point-site Voronoi diagram has linear combinatorial complexity; algorithms such as Fortune’s sweep construct it in O(n log n). Half-plane clipping is used here because its individual decisions are easier to inspect. A nearest-site query in this demo scans all sites in O(n).
The clipping operation
Walk around the current polygon. Keep a vertex when it is on the allowed side; when an edge crosses the bisector, insert its intersection. The executable model below uses the same midpoint-based signed test as the player. Coordinates are finite and sites have already been validated.
Show the executable model
function side(p, a, b) {
return (p.x-(a.x+b.x)/2)*(b.x-a.x)
+ (p.y-(a.y+b.y)/2)*(b.y-a.y);
}
function clipCell(polygon, a, b) {
const out = [];
for (let i=0; i<polygon.length; i++) {
const p=polygon[i], q=polygon[(i+1)%polygon.length];
const fp=side(p,a,b), fq=side(q,a,b);
if (fp<=0) out.push(p);
if ((fp<=0)!==(fq<=0)) {
const t=fp/(fp-fq);
out.push({x:p.x+t*(q.x-p.x), y:p.y+t*(q.y-p.y)});
}
}
return out.filter((p,i)=> {
const prev=out[(i+out.length-1)%out.length];
return Math.hypot(p.x-prev.x,p.y-prev.y)>1e-8;
});
}
function voronoi(sites) {
return sites.map((a,i)=> {
let cell=[{x:0,y:0},{x:100,y:0},{x:100,y:70},{x:0,y:70}];
sites.forEach((b,j)=> { if(i!==j) cell=clipCell(cell,a,b); });
return cell;
});
}