🕸️ Voronoi Diagrams & Lloyd's Relaxation
Interactive Voronoi diagram simulator with Lloyd's relaxation. Generate random sites, watch cells relax toward a centroidal Voronoi tessellation, and overlay the Delaunay dual graph.
🕸️ What It Demonstrates
A Voronoi diagram partitions the plane into regions ("cells") according to a set of scattered points called sites: every point in a given cell is closer to that cell's site than to any other. This simulation computes the diagram directly by rasterising — for every sample point across the canvas it performs a brute-force nearest-site search over all sites and colours that sample by the winning site's index, which is the simplest possible way to build a mathematically correct Voronoi diagram without implementing a sweep-line or divide-and-conquer algorithm.
Lloyd's algorithm (also called Lloyd's relaxation) repeatedly improves the layout of the sites: for each cell, it computes the centroid (average position) of every raster sample currently assigned to that cell, then moves the site to that centroid. Recomputing the diagram after each move and repeating this process causes the tessellation to converge toward a centroidal Voronoi tessellation (CVT) — a configuration where every site sits exactly at its own cell's centre of mass, producing cells that are more uniform in size and closer to regular hexagons.
How to Use
- Drag the Point Count slider (10–150) to change the number of random sites; click Randomize Points to generate a fresh set at any time (this also resets the iteration count).
- Set Iterations / Click and press Run Lloyd's Relaxation to perform that many centroid-relaxation steps at once.
- Toggle Auto-relax to continuously run one relaxation step per animation frame and watch the cells settle live; toggle it off at any time.
- Enable Show Delaunay triangulation overlay to draw the dual graph: a line is drawn between two sites whenever their cells share a raster boundary, revealing the Delaunay triangulation that is mathematically dual to the Voronoi diagram.
- Watch the Uniformity statistic — the coefficient of variation of cell areas — trend toward zero as more relaxation iterations run, indicating the cells are becoming more equal in size.
Did You Know?
Lloyd's relaxation has real applications far beyond pretty patterns. In computer graphics and finite-element meshing it is used to generate well-shaped triangular or polygonal meshes with fewer slivers and more uniform element sizes, which improves numerical stability. In image processing, weighted Voronoi stippling (popularised by Adrian Secord in 2002) uses Lloyd's relaxation to scatter stipple dots so that denser regions of an image get more dots while every dot ends up evenly spaced from its neighbours, mimicking the look of hand-drawn pointillist art. Perhaps most surprisingly, Stuart Lloyd described essentially the same iterative scheme — assign points to the nearest centre, recompute each centre as the mean of its assigned points, repeat — for both continuous Voronoi relaxation and for what became known as the k-means clustering algorithm, in a 1957 Bell Labs technical report on pulse-code modulation quantisation (not widely published until 1982). Voronoi relaxation and k-means are, at their core, the same algorithm applied to different kinds of data.
About this simulation
This simulator computes Voronoi diagrams by rasterising the canvas and, for every sample, brute-force searching for the nearest of up to 150 random sites. Pressing Run Lloyd's Relaxation moves every site to the true centroid of the raster samples currently assigned to it — a genuine implementation of Lloyd's algorithm — and repeating this drives the layout toward a centroidal Voronoi tessellation with more uniform, hexagon-like cells.
🔬 What it shows
An offscreen sample grid (roughly one sample per 3×3 pixel block) is scanned; each sample finds its nearest site by Euclidean distance and is coloured accordingly, while its coordinates accumulate into that site's running centroid sum. Neighbouring samples assigned to different sites mark those two sites as adjacent, which drives the Delaunay overlay.
🎮 How to use
Set the Point Count slider and click Randomize Points to seed new sites. Choose Iterations/Click and press Run Lloyd's Relaxation, or flip on Auto-relax to watch continuous convergence. Toggle Show Delaunay triangulation overlay to see the dual graph, and watch the Uniformity (coefficient of variation of cell area) statistic fall as cells even out.
💡 Did you know?
Stuart Lloyd's 1957 Bell Labs report describing this centroid-relaxation scheme is the same algorithm now known as k-means clustering — assign to nearest centre, recompute the centre as a mean, repeat. It is also the basis of Secord's 2002 weighted Voronoi stippling technique for generating pointillist-style images.
Frequently asked questions
What is a Voronoi diagram?
A Voronoi diagram divides a plane into regions, one per input site, such that every point inside a region is closer to that region's site than to any other site. This simulation builds the diagram by sampling the canvas on a grid and, for each sample, brute-force checking the distance to every site to find the nearest one.
What does Lloyd's relaxation actually do?
For every site, it computes the centroid — the average x and y coordinate — of all raster samples currently assigned to that site's cell, then moves the site to that centroid. Recomputing the Voronoi diagram after each move and repeating the process is the full Lloyd's algorithm; it is not a random nudge, it genuinely averages the cell's raster samples.
Why do the cells become more uniform over time?
Moving each site to its own cell's centre of mass reduces the variance in how compact or lopsided cells are. Repeating this iteratively converges toward a centroidal Voronoi tessellation, where every site coincides with its cell's centroid and cells tend toward similar areas and roughly hexagonal shapes, similar to patterns seen in biological cell packing and soap froths.
How is the Delaunay triangulation overlay computed?
Rather than running a separate Delaunay algorithm, the simulation exploits duality: while scanning the raster grid, whenever two neighbouring samples belong to different sites, those two sites are recorded as adjacent. Drawing a line between every adjacent pair reconstructs the Delaunay triangulation, which is mathematically the dual graph of the Voronoi diagram.
What is the Uniformity statistic measuring?
It is the coefficient of variation (standard deviation divided by mean) of the cell areas, where each cell's area is approximated by the number of raster samples assigned to it. A value near zero means all cells are close to the same size; the value generally decreases as more Lloyd relaxation iterations are applied.
Generate random sites and watch a raster-computed Voronoi diagram relax toward a centroidal tessellation using real Lloyd's algorithm, with a Delaunay dual overlay and live cell-uniformity statistics.
2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install