Scatter seeds, give every pixel to its nearest seed, then slide each
seed to the centre of its territory. Do it over and over, and chaos relaxes into a
near-perfect hexagonal honeycomb. This is Lloyd's algorithm for a
centroidal Voronoi tessellation, drawn as stained glass.
From noise to order
Below is one run from the first instant. At iter 0 the seeds sit
anywhere: cells are lopsided, some huge, some pinpricks, colours jostling. As each
seed walks to the centroid (centre of mass) of its cell, the sizes equalise, the
walls straighten, and the topology quietly reorganises until almost every cell has
six sides — the most efficient tiling of the plane. Scrub it, or press play.
iter 0
loading…
23 frames, one every 2 iterations, from 260 seeds scattered at random
(iter 0) to a relaxed honeycomb. Each seed keeps its starting colour, so you can watch
cells trade neighbours as they rearrange.
Fully settled: 320 seeds, each a jewel of colour with dark
grout between. The bevel catches light from the upper-left, so cells read as
slightly domed glass.A crop from the centre. Fewer, larger cells let you see the
grout thickness — it is the gap between the two nearest seeds
1−(d₂−d₁)³ — and the directional highlight.
How I built it
Nearest-seed paint. For every pixel, find the closest seed and
fill it with that seed's colour. Naively that is O(NP) per pixel
(~320 seeds × 620² pixels), OpenMP-parallel over rows; at this size it is
fast enough to render a full relaxation in ~15 s.
Centroid move. Accumulate, per cell, the summed pixel
coordinates and count, then set each seed to that cell's centre of mass. This is
exactly the Lloyd step: it minimises the average squared distance from any point to
its nearest seed, so it provably descends toward the regular hexagonal pattern.
Stained glass. A seed's colour comes from an HSV ramp across
the hue wheel with a little jitter, so neighbours never clash too hard. The grout
(the dark wall) is thin where a pixel is far from the second-nearest seed, i.e. deep
inside a cell, and dark where two cells press together. A directional light adds a
bright rim on the grout facing the top-left, giving each panel a bevelled, glassy
feel.
The hexagon is not imposed. Nothing in the code says "make
hexagons." Six-sided cells are simply the fixed point of the centroid step —
order from a rule that only asks each seed to sit at the balance point of its
territory.