Voronoi Diagram
What is a Voronoi diagram?
A Voronoi diagram (also called a Voronoi tessellation, Dirichlet tessellation or Thiessen polygons) divides a plane into one region per point, called a site. Each region, or Voronoi cell, holds every location that is closer to its own site than to any other. The boundaries are where two sites are equally close.

Voronoi cells around 24 randomly placed sites on a 100 × 100 km map (sample data). Try the interactive version.
- A Voronoi cell is the set of points nearer to its site than to any other site, so the diagram is a map of "which one is nearest?".
- With straight-line distance, every edge lies on the perpendicular bisector of two sites, and every vertex is equally far from three sites.
- The Delaunay triangulation joins the sites whose cells touch, so it carries the same information as the Voronoi diagram.
- Change the distance measure and the cells change shape. City-block (Manhattan) cells have only horizontal, vertical and 45° edges.
- Cells at the edge of the data are open-ended; their size depends on where you crop the map, not on the data.
How a Voronoi diagram works
Start with a set of sites, such as fire stations, phone masts or the centres of clusters. For any location on the map, find the nearest site. Do that for every location and colour each one by its nearest site, and the colours form the cells.
- Site. The point each cell is built around, also called a seed or generator.
- Cell. Everything nearer to that site than to any other. A location on the boundary is equally near two sites; at a vertex, three.
- Edge. With ordinary straight-line distance, each edge is part of the perpendicular bisector of two sites: the line through the midpoint between them, at right angles to the line joining them. The two sites are mirror images across it.
- Vertex. Where three cells meet, the vertex is the same distance from all three sites. A circle centred there passes through the three sites and has no other site inside it (if it did, that site would be nearer, and the vertex would belong to its cell).
- Open cell. Sites on the outside of the group (on its convex hull, the shape a rubber band stretched around all the sites would take) have cells that run off to infinity. A chart has to cut them at a frame.
In symbols, with sites and a distance , the cell of site is
Some properties follow from that definition and are useful when you check a diagram:
- With straight-line distance, every cell is a convex polygon: a line between two points in a cell never leaves it. Each cell is the overlap of half-planes, one for each other site.
- For sites that don't all lie on one line, there are at most vertices and edges. On average a cell has fewer than six neighbours; for many randomly scattered sites the average approaches six.
- A site's nearest other site is always one of its neighbours across a cell edge.
- Moving one site only changes its own cell and the cells of its neighbours, before and after the move. The rest of the diagram stays put.
A worked example: three fire stations
Three fire stations stand at A (0, 0), B (6, 0) and C (0, 4), in kilometres. Each station should cover the area it can reach first, which, as a first approximation, is the area nearest to it in a straight line.
- The A–B boundary is the perpendicular bisector of A and B: the vertical line .
- The A–C boundary is the horizontal line .
- The B–C boundary is the set of points equally far from B and C: , which simplifies to .
The three lines meet at (3, 2), the Voronoi vertex. It is km from all three stations. Triangle ABC has a right angle at A, so its circumcentre is the midpoint of the long side BC, which is (3, 2) again.
Station A covers the lower-left quarter, and . B covers everything to the right of and below the B–C line, and C covers the rest. A call from (4, 3.2) is 5.12 km from A, 3.77 km from B and 4.08 km from C, so B is nearest. Measured along a street grid instead (the Manhattan distance, below), the same call is 7.2 km from A, 5.2 km from B and 4.8 km from C, and C is nearest.
Voronoi diagram and Delaunay triangulation
Draw a line between every pair of sites whose cells share an edge. The lines divide the area between the sites into triangles, and that set of triangles is the Delaunay triangulation, described by Boris Delaunay in 1934. The two structures are duals: each cell matches a site, each Voronoi edge matches a Delaunay edge (crossing it at a right angle where they meet), and each Voronoi vertex matches a Delaunay triangle.
The defining test is the empty circle property: the circle through the three corners of a Delaunay triangle (its circumcircle) has no other site inside it. Its centre is the Voronoi vertex where the three sites' cells meet. Of all the ways to triangulate a set of points, the Delaunay triangulation makes the smallest angle as large as possible, so it avoids long, thin triangles, which makes it the usual choice for finite-element meshes, terrain models and interpolating scattered measurements.
In practice you build one and read the other off it. Many libraries compute the Delaunay triangulation and return the Voronoi diagram as its dual, by joining the circumcentres of neighbouring triangles. The demo on this page does the reverse: it computes the cells, then takes the Delaunay edges from which cells touch.
How to make a Voronoi diagram
There are five common ways, from pencil and paper to graphics hardware.
| Method | How it works | Cost for n sites | Good for |
|---|---|---|---|
| By hand, with bisectors | Draw the perpendicular bisector of each pair of nearby sites and keep the parts nearest each site | Practical up to a dozen or so sites | Teaching, sketches, checking a result |
| Half-plane clipping | Start each cell as a large rectangle and cut away the far side of the bisector with every other site | About clips; fast for hundreds of sites | Small interactive diagrams; the demo on this page |
| Delaunay first, then the dual | Triangulate the sites (for example by adding points one at a time and fixing triangles whose circumcircle holds the new point), then join circumcentres | expected with randomised insertion | General use; what most geometry libraries do |
| Fortune's sweep line | Sweep a line across the plane, keeping a "beach line" of parabolic arcs, and record a vertex each time an arc shrinks to nothing | , which is optimal | Large point sets |
| Nearest-site grid (rasterising) | Test every pixel or grid square for its nearest site and colour it | Grid squares × sites; graphics cards do it in parallel | Any distance measure, weighted sites, heatmaps |
Fortune's algorithm (Steven Fortune, 1987) is the classic fast method. A horizontal line sweeps down over the sites. The points already settled are those closer to some site above the line than to the line itself, and the boundary of that region is made of parabolic arcs, one per site. Two kinds of event change it: a new site reaching the line adds an arc, and an arc shrinking to nothing marks a Voronoi vertex. Kept in a balanced tree and a priority queue, the arcs and events give the whole diagram in time.
The grid method is the simplest to write and the most flexible. Each square knows its nearest site, but the edges are only as sharp as the grid. Hoff and colleagues showed in 1999 that graphics hardware can draw it by rendering a cone under each site and letting the depth test keep the nearest. The heatmap modes in the demo use this method on a 200 × 200 grid, which is how they handle Manhattan distance.
Euclidean vs Manhattan Voronoi diagrams
Everything above assumes straight-line, or Euclidean, distance. A Voronoi diagram can use any distance, and the cells reflect it.
| Distance | Formula | What the cells look like | When it fits |
|---|---|---|---|
| Euclidean | Convex polygons; edges at any angle | Travel in a straight line: radio signals, open ground, most data sets | |
| Manhattan (city block, L1) | Edges only horizontal, vertical or at 45°; cells can be non-convex | Travel on a grid of streets, chip and circuit layout | |
| Chebyshev (L∞) | Also horizontal, vertical and 45° edges, but in different places from the Manhattan ones | Moves where a diagonal step costs the same as a straight one | |
| Weighted: power | Squared distance minus a weight for each site | Straight edges; a weak site can lose its cell entirely | Masts of different power, shops of different appeal |
| Weighted: multiplicative | Distance divided by a weight for each site | Edges are circular arcs; a weak site keeps only a small cell | Masts of different power, shops of different appeal |
| Along a network | Shortest path on roads or pipes | Cells follow the network | Real travel times, service areas |
Manhattan cells have one quirk. When two sites are diagonal to each other, with equal horizontal and vertical offsets, a whole region can be the same distance from both, so the boundary is not a line but an area. Software has to break the tie. The demo on this page gives such points to the first site in the list.
Voronoi diagram examples and uses
Voronoi diagrams answer one question, "which of these is nearest?", for every point at once. That makes them useful far beyond geometry:
- Service areas and catchments. The area each school, hospital, shop or depot is nearest to is a first estimate of who it serves. In hydrology, the meteorologist Alfred Thiessen used the same construction in 1911 to weight rain gauges by the area each one represents, which is why GIS tools still call the cells Thiessen polygons.
- Mobile phone coverage. A phone usually connects to the strongest signal, which, for masts of similar power on open ground, is roughly the nearest one. A Voronoi diagram of the masts is a common first model of each cell's coverage.
- The 1854 cholera outbreak in London. In a map published in 1855, John Snow drew a dotted line around the area that was closer, by walking distance along the streets, to the Broad Street pump than to any other pump. Deaths fell away sharply, or stopped, outside it. That boundary is a Voronoi cell measured along a street network.
- Machine learning. A 1-nearest-neighbour classifier gives each point the label of its nearest training example, so its decision boundary is made of Voronoi edges between examples of different classes. In k-means clustering, each point is assigned to its nearest centre, so the clusters at each step are the Voronoi cells of the centres. See the decision boundary guide for how other classifiers divide the plane.
- Materials and biology. If crystals start growing at random points at the same moment and at the same speed, the grains they form when they meet are Voronoi cells. Real metals deviate from this, but it is a standard starting model for grain structure, and similar models describe packed cells in plant and animal tissue.
- Computer graphics. Worley noise, a texture function published in 1996, shades each pixel by its distance to the nearest of a set of random points. It is behind many stone, scale and cell textures in games and films.
- Robotics. The edges of a Voronoi diagram built from obstacles are the paths that stay as far from the obstacles as possible, so they are used for safe route planning.
- Even spacing. Lloyd's algorithm moves each site to the centre of its own cell and repeats. The sites spread out evenly, giving a centroidal Voronoi tessellation, used for stippling images, placing sensors and meshing.
Voronoi diagram vs heatmap vs choropleth map
A Voronoi diagram, a heatmap and a choropleth map can all look like a patchwork of coloured regions. They answer different questions.
| Chart | Regions come from | Colour shows | Use it when |
|---|---|---|---|
| Voronoi diagram | The positions of the sites: each region is the area nearest one site | Which site, or a property of the site or cell (area, load, class) | You want to see who serves what, nearest neighbours, or how evenly points are spread |
| Heatmap | A regular grid of equal squares | A value measured or computed at each square | You have a value at every point of a grid, such as density or temperature |
| Choropleth map | Fixed boundaries: countries, states, postcodes | A rate or class for each area | Your data is already grouped by named areas |
| Scatter plot | No regions; each point is drawn on its own | Category or value of each point | The points matter more than the space between them |
A nearest-site heatmap sits between the first two: a grid coloured by the Voronoi cell each square falls in.
Common Voronoi diagram mistakes
Most Voronoi diagram mistakes come from trusting the cells more than the sites, distances and scales behind them.
- Unequal axis scales. If one unit on the x axis is drawn longer than one unit on the y axis, the bisectors no longer look perpendicular and the cells look wrong, even though the maths is right. Lock the aspect ratio.
- Reading the edge cells as real areas. Open cells are cut wherever the frame happens to be. With randomly placed sites, the largest cell is usually one at the edge for that reason. Compare only closed cells, or clip to a meaningful boundary such as a coastline or a city limit.
- Using straight-line distance for travel that isn't straight. Customers, ambulances and pipes follow networks. A straight-line catchment can put a house in the wrong area when a river or motorway lies between it and the nearest site.
- Mixing units. When the axes are different quantities, such as age and income in a nearest-neighbour model, the cells depend entirely on how each axis is scaled. Standardise the features first.
- Ignoring duplicates and ties. Two sites in the same place share one cell, and four or more sites on a circle make a vertex where four cells meet. Code that draws Voronoi diagrams should check for both.
- Giving neighbouring cells the same colour. Colour by site with a palette where touching cells always differ; a greedy colouring over the Delaunay neighbours does this.
- Treating cell area as importance. A small cell means the sites are crowded there, not that the site matters less. Colour by area to show spacing, not value.
How to make a Voronoi diagram in JavaScript, React or Angular
In the demo below, the cells, the outlines, the Delaunay overlay and a 200 by 200 nearest-site heatmap (40,000 squares) are recomputed and redrawn as you drag a site, with up to 200 sites. The demo computes every cell in the browser by half-plane clipping, with no geometry library.
Click an empty spot to add a site, drag a site to move it, and Shift+click a site to remove it. Draw cells as switches between exact polygons and a nearest-site heatmap with Euclidean or Manhattan distance; Colour shows each site in its own colour or shades each cell by its area. Delaunay triangulation overlays the dual, and Euclidean cell edges draws the polygon outlines, which on the Manhattan heatmap shows how far the two diagrams differ. Sites, New random sites and Clear start again. The readout gives the number of cells, the largest and smallest cell, the Delaunay counts and the nearest site to the pointer. Scroll to zoom, right-drag to pan and double-click to reset.
Here is the code for this chart. The Voronoi cells, the Delaunay edges and the nearest-site grid are plain JavaScript; SciChart.js draws the cells as triangle series, the heatmap as a uniform heatmap, the outlines as line series and the sites as scatter markers.
- JavaScript
- React
- Angular
// An interactive Voronoi diagram with SciChart.js: click to add a site, drag a site to move it, Shift+click to remove it
const {
SciChartSurface,
NumericAxis,
NumberRange,
XyDataSeries,
FastTriangleRenderableSeries,
ETriangleSeriesDrawMode,
FastLineRenderableSeries,
XyScatterRenderableSeries,
EllipsePointMarker,
UniformHeatmapDataSeries,
UniformHeatmapRenderableSeries,
HeatmapColorMap,
CustomChartModifier2D,
translateFromCanvasToSeriesViewRect,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier,
EExecuteOn,
SciChartJsNavyTheme
} = SciChart;
// or, for npm, import { SciChartSurface, ... } from "scichart"
// --- Voronoi maths (plain JavaScript, no library needed) ---
// Clip a convex polygon to the side of a line where side(x, y) <= 0 (Sutherland-Hodgman).
// Each vertex carries `edge`: what made the edge from it to the next vertex (a site index, or -1 for the frame).
function clipPolygon(polygon, side, newEdge) {
const out = [];
for (let i = 0; i < polygon.length; i++) {
const p = polygon[i];
const q = polygon[(i + 1) % polygon.length];
const sp = side(p.x, p.y);
const sq = side(q.x, q.y);
const cut = () => {
const t = sp / (sp - sq);
return { x: p.x + t * (q.x - p.x), y: p.y + t * (q.y - p.y) };
};
if (sp <= 0) {
out.push(p);
if (sq > 0) out.push({ ...cut(), edge: newEdge }); // leaving: the next edge runs along the cutting line
} else if (sq <= 0) {
out.push({ ...cut(), edge: p.edge }); // entering: the next edge is what is left of p -> q
}
}
// Drop zero-length edges (a vertex lying exactly on the cutting line gives a duplicate point)
return out.filter((v, i) => {
const w = out[(i + 1) % out.length];
return out.length < 2 || Math.hypot(v.x - w.x, v.y - w.y) > 1e-9;
});
}
function polygonArea(polygon) {
let sum = 0;
for (let i = 0; i < polygon.length; i++) {
const p = polygon[i];
const q = polygon[(i + 1) % polygon.length];
sum += p.x * q.y - q.x * p.y; // shoelace formula
}
return Math.abs(sum) / 2;
}
// Voronoi cells by half-plane intersection. Each cell starts as a huge square; for every other site, nearest
// first, cut away the half beyond the perpendicular bisector of the two sites. O(n²): fine for a few hundred
// sites. (For many thousands, use Fortune's sweep line or a Delaunay library and take the dual.)
// Returns, per site: its cell cut to the map, the area, the neighbours (its Delaunay edges) and the full cell.
function voronoiCells(sites) {
const far = 1000 * SIZE; // big enough that every bisector crossing the map is seen
return sites.map((a, i) => {
let cell = [
{ x: -far, y: -far, edge: -1 },
{ x: far, y: -far, edge: -1 },
{ x: far, y: far, edge: -1 },
{ x: -far, y: far, edge: -1 }
];
const others = sites
.map((b, j) => ({ j, d: Math.hypot(b.x - a.x, b.y - a.y) }))
.filter((o) => o.j !== i)
.sort((u, v) => u.d - v.d);
for (const { j, d } of others) {
// A site more than twice as far as the cell's furthest corner can't cut the cell: stop
const reach = Math.max(...cell.map((v) => Math.hypot(v.x - a.x, v.y - a.y)));
if (d > 2 * reach) break;
if (d === 0) continue; // two sites in the same place
const b = sites[j];
const nx = b.x - a.x;
const ny = b.y - a.y;
const c = (nx * (a.x + b.x) + ny * (a.y + b.y)) / 2; // the bisector: nx * x + ny * y = c
cell = clipPolygon(cell, (x, y) => nx * x + ny * y - c, j);
}
let polygon = cell;
polygon = clipPolygon(polygon, (x) => -x, -1);
polygon = clipPolygon(polygon, (x) => x - SIZE, -1);
polygon = clipPolygon(polygon, (x, y) => -y, -1);
polygon = clipPolygon(polygon, (x, y) => y - SIZE, -1);
const neighbours = [...new Set(cell.filter((v) => v.edge >= 0).map((v) => v.edge))];
return { cell, polygon, area: polygonArea(polygon), neighbours };
});
}
// The Delaunay triangulation is the dual of the Voronoi diagram: join two sites when their cells share an edge.
// Each corner of a cell where two bisectors meet is a Voronoi vertex, the centre of one Delaunay triangle.
function delaunayFromCells(cells) {
const edges = [];
const triangles = new Set();
cells.forEach((c, i) => {
for (const j of c.neighbours) if (i < j) edges.push([i, j]);
const n = c.cell.length;
for (let k = 0; k < n; k++) {
const before = c.cell[(k + n - 1) % n].edge;
const after = c.cell[k].edge;
if (before >= 0 && after >= 0 && before !== after) triangles.add([i, before, after].sort((u, v) => u - v).join());
}
});
return { edges, triangleCount: triangles.size };
}
const DISTANCE = {
euclidean: (dx, dy) => Math.hypot(dx, dy),
manhattan: (dx, dy) => Math.abs(dx) + Math.abs(dy) // city-block distance
};
// The index of the nearest site to (x, y); ties go to the lower index
function nearestSite(sites, x, y, distance) {
let best = -1;
let bestDistance = Infinity;
sites.forEach((s, i) => {
const d = distance(s.x - x, s.y - y);
if (d < bestDistance) {
bestDistance = d;
best = i;
}
});
return { index: best, distance: bestDistance };
}
// Give neighbouring cells different colours (greedy colouring over the Delaunay neighbours)
function colourBySite(cells, colourCount) {
const colours = [];
cells.forEach((c, i) => {
const used = new Set(c.neighbours.map((j) => colours[j]));
let k = 0;
while (used.has(k) && k < colourCount - 1) k++;
colours[i] = k;
});
return colours;
}
// Area classes relative to the average cell (SIZE² / number of sites)
const AREA_CLASSES = [0.25, 0.5, 0.8, 1.25, 2, 4, Infinity];
const areaClass = (area, mean) => AREA_CLASSES.findIndex((limit) => area / mean < limit);
// --- The chart ---
const SITE_COLOURS = ["#3D7DC4", "#D9772E", "#3BA56A", "#8E66C9", "#C9A227", "#2AA7A7", "#C9506E", "#6D7F9C"];
const AREA_COLOURS = ["#440154", "#443A83", "#31688E", "#21918C", "#35B779", "#90D743", "#FDE725"];
const { wasmContext, sciChartSurface } = await SciChartSurface.create(ui.chartDiv, { theme: new SciChartJsNavyTheme() });
const xAxis = new NumericAxis(wasmContext, { axisTitle: "x (km)", visibleRange: new NumberRange(0, SIZE) });
const yAxis = new NumericAxis(wasmContext, { axisTitle: "y (km)", visibleRange: new NumberRange(0, SIZE) });
sciChartSurface.xAxes.add(xAxis);
sciChartSurface.yAxes.add(yAxis);
// Heatmap mode: one value per grid cell, the colour index of the nearest site. A colour map with a sharp step
// at each whole number turns the indexes into flat colours.
const steppedColorMap = (colours) =>
new HeatmapColorMap({
minimum: 0,
maximum: colours.length,
gradientStops: colours.flatMap((color, i) => [
{ offset: i / colours.length, color },
{ offset: (i + 1) / colours.length - 1e-6, color }
])
});
const heatmapData = new UniformHeatmapDataSeries(wasmContext, {
xStart: 0,
xStep: SIZE / GRID,
yStart: 0,
yStep: SIZE / GRID,
zValues: Array.from({ length: GRID }, () => new Array(GRID).fill(0))
});
const heatmap = new UniformHeatmapRenderableSeries(wasmContext, {
dataSeries: heatmapData,
colorMap: steppedColorMap(SITE_COLOURS),
opacity: 0.8
});
sciChartSurface.renderableSeries.add(heatmap);
// Polygon mode: one triangle series per colour, each cell cut into a fan of triangles from its first corner
const fills = SITE_COLOURS.map(() => {
const dataSeries = new XyDataSeries(wasmContext, { isSorted: false });
const series = new FastTriangleRenderableSeries(wasmContext, {
dataSeries,
drawMode: ETriangleSeriesDrawMode.List, // every three points are one triangle
opacity: 0.8
});
sciChartSurface.renderableSeries.add(series);
return { series, dataSeries };
});
// Lines with NaN gaps: one series draws every cell outline, another every Delaunay edge
const lineSeries = (stroke, strokeThickness, strokeDashArray = undefined) => {
const dataSeries = new XyDataSeries(wasmContext, { isSorted: false, containsNaN: true });
const series = new FastLineRenderableSeries(wasmContext, {
dataSeries,
stroke,
strokeThickness,
strokeDashArray
});
sciChartSurface.renderableSeries.add(series);
return { series, dataSeries };
};
const cellEdges = lineSeries("#0B1424", 2);
const delaunayEdges = lineSeries("#FFD166", 2);
const frame = lineSeries("#C8D3E8", 1.5);
frame.dataSeries.appendRange([0, SIZE, SIZE, 0, 0], [0, 0, SIZE, SIZE, 0]);
const siteData = new XyDataSeries(wasmContext, { isSorted: false });
const siteMarkers = new XyScatterRenderableSeries(wasmContext, {
dataSeries: siteData,
pointMarker: new EllipsePointMarker(wasmContext, { width: 11, height: 11, fill: "#FFFFFF", stroke: "#0B1424", strokeThickness: 2 })
});
sciChartSurface.renderableSeries.add(siteMarkers);
// --- Sites and redraw ---
let seed = 1908; // seeded random numbers, so the first layout is the same every time
const random = () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
const randomSites = (count) => Array.from({ length: count }, () => ({ x: 3 + (SIZE - 6) * random(), y: 3 + (SIZE - 6) * random() }));
let sites = randomSites(24);
let hover;
function redraw() {
const mode = ui.modeSelect.value;
const byArea = ui.colourSelect.value === "area";
const cells = voronoiCells(sites);
const { edges, triangleCount } = delaunayFromCells(cells);
const meanArea = (SIZE * SIZE) / Math.max(sites.length, 1);
// Areas: exact polygons for Euclidean distance, counted grid squares for the heatmaps
let areas = cells.map((c) => c.area);
const owner = [];
if (mode !== "polygons") {
const distance = DISTANCE[mode];
const counts = new Array(sites.length).fill(0);
for (let row = 0; row < GRID; row++) {
owner.push([]);
for (let col = 0; col < GRID; col++) {
const { index } = nearestSite(sites, ((col + 0.5) * SIZE) / GRID, ((row + 0.5) * SIZE) / GRID, distance);
owner[row].push(index);
if (index >= 0) counts[index]++;
}
}
areas = counts.map((n) => (n * SIZE * SIZE) / (GRID * GRID));
}
const siteColours = colourBySite(cells, SITE_COLOURS.length);
const colourIndex = sites.map((_, i) => (byArea ? areaClass(areas[i], meanArea) : siteColours[i]));
// Fills
heatmap.isVisible = mode !== "polygons" && sites.length > 0;
if (heatmap.isVisible) {
heatmap.colorMap = steppedColorMap(byArea ? AREA_COLOURS : SITE_COLOURS);
heatmapData.setZValues(owner.map((row) => row.map((i) => colourIndex[i] + 0.5)));
}
fills.forEach(({ series, dataSeries }, k) => {
const xs = [];
const ys = [];
if (mode === "polygons") {
cells.forEach((c, i) => {
if (colourIndex[i] !== k) return;
for (let v = 1; v + 1 < c.polygon.length; v++) {
for (const p of [c.polygon[0], c.polygon[v], c.polygon[v + 1]]) {
xs.push(p.x);
ys.push(p.y);
}
}
});
}
dataSeries.clear();
dataSeries.appendRange(xs, ys);
series.fill = (byArea ? AREA_COLOURS : SITE_COLOURS)[k] ?? "#000000";
});
// Lines
const outline = { x: [], y: [] };
for (const c of cells) {
for (const p of [...c.polygon, c.polygon[0]]) {
outline.x.push(p ? p.x : NaN);
outline.y.push(p ? p.y : NaN);
}
outline.x.push(NaN);
outline.y.push(NaN);
}
cellEdges.dataSeries.clear();
cellEdges.dataSeries.appendRange(outline.x, outline.y);
cellEdges.series.isVisible = ui.edgesCheckbox.checked;
const dual = { x: [], y: [] };
for (const [i, j] of edges) {
dual.x.push(sites[i].x, sites[j].x, NaN);
dual.y.push(sites[i].y, sites[j].y, NaN);
}
delaunayEdges.dataSeries.clear();
delaunayEdges.dataSeries.appendRange(dual.x, dual.y);
delaunayEdges.series.isVisible = ui.delaunayCheckbox.checked;
siteData.clear();
siteData.appendRange(
sites.map((s) => s.x),
sites.map((s) => s.y)
);
summary = { areas, edgeCount: edges.length, triangleCount, byArea, mode };
updateStatus();
}
// The readout under the chart: the cells, the Delaunay counts and the nearest site to the pointer
let summary;
function updateStatus() {
if (sites.length === 0) {
ui.status.textContent = "No sites. Click the chart to add one.";
return;
}
const { areas, edgeCount, triangleCount, byArea, mode } = summary;
const largest = areas.indexOf(Math.max(...areas));
const smallest = areas.indexOf(Math.min(...areas));
const km2 = (a) => `${Math.round(a).toLocaleString("en-GB")} km²`;
const share = (a) => `${((100 * a) / (SIZE * SIZE)).toFixed(1)}% of the map`;
let text =
`${sites.length} cells · largest: site ${largest + 1}, ${km2(areas[largest])} (${share(areas[largest])}) · ` +
`smallest: site ${smallest + 1}, ${km2(areas[smallest])} · Delaunay: ${edgeCount} edges, ${triangleCount} triangles, ` +
`${((2 * edgeCount) / sites.length).toFixed(2)} neighbours per cell on average`;
if (byArea) text += " · colour: cell area from under ¼ of the average (dark) to over 4× (bright)";
if (hover) {
const metric = mode === "manhattan" ? "manhattan" : "euclidean";
const { index, distance } = nearestSite(sites, hover.x, hover.y, DISTANCE[metric]);
text +=
`\nPointer at (${hover.x.toFixed(1)}, ${hover.y.toFixed(1)}): nearest is site ${index + 1}, ` +
`${distance.toFixed(1)} km away (${metric === "manhattan" ? "Manhattan" : "Euclidean"} distance)`;
}
ui.status.textContent = text;
}
// --- Editing sites with the mouse: a custom chart modifier ---
class SiteEditorModifier extends CustomChartModifier2D {
dragIndex = -1;
pendingAdd;
toData(args) {
const point = translateFromCanvasToSeriesViewRect(args.mousePoint, this.parentSurface.seriesViewRect);
if (!point) return undefined;
return {
x: xAxis.getCurrentCoordinateCalculator().getDataValue(point.x),
y: yAxis.getCurrentCoordinateCalculator().getDataValue(point.y),
pixelsPerKm: this.parentSurface.seriesViewRect.width / xAxis.visibleRange.diff
};
}
// The site under the pointer, within 10 pixels
siteAt(p) {
const { index, distance } = nearestSite(sites, p.x, p.y, DISTANCE.euclidean);
return index >= 0 && distance * p.pixelsPerKm < 10 ? index : -1;
}
modifierMouseDown(args) {
super.modifierMouseDown(args);
if (args.button !== EExecuteOn.MouseLeftButton) return;
const p = this.toData(args);
if (!p) return;
const index = this.siteAt(p);
if (this.pendingAdd) {
// Second click of a double-click: let ZoomExtentsModifier reset the zoom instead of adding sites
clearTimeout(this.pendingAdd);
this.pendingAdd = undefined;
return;
}
if (index >= 0 && args.shiftKey) {
sites.splice(index, 1); // Shift+click removes a site
redraw();
} else if (index >= 0) {
this.dragIndex = index;
} else if (p.x >= 0 && p.x <= SIZE && p.y >= 0 && p.y <= SIZE && sites.length < 200) {
this.pendingAdd = setTimeout(() => {
this.pendingAdd = undefined;
sites.push({ x: p.x, y: p.y });
redraw();
}, 300);
}
args.handled = true;
}
modifierMouseMove(args) {
super.modifierMouseMove(args);
const p = this.toData(args);
if (!p) return;
hover = p;
if (this.dragIndex >= 0) {
sites[this.dragIndex] = { x: Math.min(SIZE, Math.max(0, p.x)), y: Math.min(SIZE, Math.max(0, p.y)) };
args.handled = true;
redraw();
} else {
updateStatus();
}
}
modifierMouseUp(args) {
super.modifierMouseUp(args);
this.dragIndex = -1;
}
modifierMouseLeave(args) {
super.modifierMouseLeave(args);
this.dragIndex = -1;
hover = undefined;
updateStatus();
}
}
// Keep one km the same length on both axes, so cells keep their true shape and bisectors stay at right angles
// (checked before and after each draw: on the very first draw the layout isn't known until the end)
const keepAspect = () => {
const rect = sciChartSurface.seriesViewRect;
if (!rect || !rect.width || !rect.height) return; // not laid out yet
const kmPerPixel = Math.max(xAxis.visibleRange.diff / rect.width, yAxis.visibleRange.diff / rect.height);
const fit = (range, pixels) => {
const centre = (range.min + range.max) / 2;
return new NumberRange(centre - (kmPerPixel * pixels) / 2, centre + (kmPerPixel * pixels) / 2);
};
const newX = fit(xAxis.visibleRange, rect.width);
const newY = fit(yAxis.visibleRange, rect.height);
if (Math.abs(newX.diff - xAxis.visibleRange.diff) > 1e-6) xAxis.visibleRange = newX;
if (Math.abs(newY.diff - yAxis.visibleRange.diff) > 1e-6) yAxis.visibleRange = newY;
};
sciChartSurface.preRender.subscribe(keepAspect);
sciChartSurface.rendered.subscribe(keepAspect);
sciChartSurface.chartModifiers.add(
new SiteEditorModifier(), // first, so it can claim left clicks on sites
new MouseWheelZoomModifier(),
new ZoomPanModifier({ executeCondition: { button: EExecuteOn.MouseRightButton } }), // right-drag to pan
new ZoomExtentsModifier()
);
// --- The controls ---
ui.modeSelect.addEventListener("change", redraw);
ui.colourSelect.addEventListener("change", redraw);
ui.delaunayCheckbox.addEventListener("change", redraw);
ui.edgesCheckbox.addEventListener("change", redraw);
ui.randomButton.addEventListener("click", () => {
sites = randomSites(Number(ui.countSelect.value));
redraw();
});
ui.countSelect.addEventListener("change", () => ui.randomButton.click());
ui.clearButton.addEventListener("click", () => {
sites = [];
redraw();
});
redraw();
import { SciChartReact } from "scichart-react";
import {
SciChartSurface,
NumericAxis,
NumberRange,
XyDataSeries,
FastTriangleRenderableSeries,
ETriangleSeriesDrawMode,
FastLineRenderableSeries,
XyScatterRenderableSeries,
EllipsePointMarker,
SciChartJsNavyTheme,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier
} from "scichart";
type Point = { x: number; y: number };
// Cut a convex polygon to the side of a line where side(p) <= 0
const clip = (polygon: Point[], side: (p: Point) => number) => {
const out: Point[] = [];
polygon.forEach((p, i) => {
const q = polygon[(i + 1) % polygon.length];
const sp = side(p), sq = side(q);
if (sp <= 0) out.push(p);
if ((sp <= 0) !== (sq <= 0)) {
const t = sp / (sp - sq);
out.push({ x: p.x + t * (q.x - p.x), y: p.y + t * (q.y - p.y) });
}
});
return out;
};
// Each site's cell: the box, cut by the perpendicular bisector with every other site
const voronoiCells = (sites: Point[], size: number) =>
sites.map((a, i) => {
let cell: Point[] = [{ x: 0, y: 0 }, { x: size, y: 0 }, { x: size, y: size }, { x: 0, y: size }];
sites.forEach((b, j) => {
if (j === i) return;
const nx = b.x - a.x, ny = b.y - a.y;
const c = (nx * (a.x + b.x) + ny * (a.y + b.y)) / 2;
cell = clip(cell, (p) => nx * p.x + ny * p.y - c);
});
return cell;
});
// initChart builds the chart; SciChartReact creates it, sizes it and cleans it up
const initChart = async (rootElement: string | HTMLDivElement) => {
const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
theme: new SciChartJsNavyTheme()
});
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { axisTitle: "x (km)", visibleRange: new NumberRange(0, 100) }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { axisTitle: "y (km)", visibleRange: new NumberRange(0, 100) }));
const sites: Point[] = getYourSites(); // e.g. [{ x: 12, y: 40 }, ...] inside the 100 x 100 box
const cells = voronoiCells(sites, 100);
const colours = ["#3D7DC4", "#D9772E", "#3BA56A", "#8E66C9", "#C9A227", "#2AA7A7"];
cells.forEach((cell, i) => {
const xs: number[] = [], ys: number[] = [];
for (let v = 1; v + 1 < cell.length; v++) {
for (const p of [cell[0], cell[v], cell[v + 1]]) { xs.push(p.x); ys.push(p.y); } // a fan of triangles
}
sciChartSurface.renderableSeries.add(
new FastTriangleRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues: xs, yValues: ys, isSorted: false }),
drawMode: ETriangleSeriesDrawMode.List,
fill: colours[i % colours.length],
opacity: 0.8
}),
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: [...cell, cell[0]].map((p) => p.x),
yValues: [...cell, cell[0]].map((p) => p.y),
isSorted: false
}),
stroke: "#0B1424",
strokeThickness: 2
})
);
});
sciChartSurface.renderableSeries.add(
new XyScatterRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues: sites.map((s) => s.x), yValues: sites.map((s) => s.y), isSorted: false }),
pointMarker: new EllipsePointMarker(wasmContext, { width: 11, height: 11, fill: "#FFFFFF", stroke: "#0B1424" })
})
);
sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier(), new ZoomPanModifier(), new ZoomExtentsModifier());
return { sciChartSurface };
};
export const VoronoiDiagram = () => (
<SciChartReact initChart={initChart} style={{ width: "100%", aspectRatio: "1 / 1", maxWidth: 600 }} />
);
import { Component } from "@angular/core";
import { ScichartAngularComponent } from "scichart-angular";
import {
SciChartSurface,
NumericAxis,
NumberRange,
XyDataSeries,
FastTriangleRenderableSeries,
ETriangleSeriesDrawMode,
FastLineRenderableSeries,
XyScatterRenderableSeries,
EllipsePointMarker,
SciChartJsNavyTheme,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier
} from "scichart";
type Point = { x: number; y: number };
// Cut a convex polygon to the side of a line where side(p) <= 0
const clip = (polygon: Point[], side: (p: Point) => number) => {
const out: Point[] = [];
polygon.forEach((p, i) => {
const q = polygon[(i + 1) % polygon.length];
const sp = side(p), sq = side(q);
if (sp <= 0) out.push(p);
if ((sp <= 0) !== (sq <= 0)) {
const t = sp / (sp - sq);
out.push({ x: p.x + t * (q.x - p.x), y: p.y + t * (q.y - p.y) });
}
});
return out;
};
// Each site's cell: the box, cut by the perpendicular bisector with every other site
const voronoiCells = (sites: Point[], size: number) =>
sites.map((a, i) => {
let cell: Point[] = [{ x: 0, y: 0 }, { x: size, y: 0 }, { x: size, y: size }, { x: 0, y: size }];
sites.forEach((b, j) => {
if (j === i) return;
const nx = b.x - a.x, ny = b.y - a.y;
const c = (nx * (a.x + b.x) + ny * (a.y + b.y)) / 2;
cell = clip(cell, (p) => nx * p.x + ny * p.y - c);
});
return cell;
});
@Component({
selector: "app-voronoi-diagram",
standalone: true,
imports: [ScichartAngularComponent],
template: `<scichart-angular [initChart]="initChart" style="display: block; width: 100%; max-width: 600px; aspect-ratio: 1 / 1"></scichart-angular>`
})
export class VoronoiDiagramComponent {
// initChart builds the chart; scichart-angular creates it, sizes it and cleans it up
initChart = async (rootElement: string | HTMLDivElement) => {
const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
theme: new SciChartJsNavyTheme()
});
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { axisTitle: "x (km)", visibleRange: new NumberRange(0, 100) }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { axisTitle: "y (km)", visibleRange: new NumberRange(0, 100) }));
const sites: Point[] = getYourSites(); // e.g. [{ x: 12, y: 40 }, ...] inside the 100 x 100 box
const cells = voronoiCells(sites, 100);
const colours = ["#3D7DC4", "#D9772E", "#3BA56A", "#8E66C9", "#C9A227", "#2AA7A7"];
cells.forEach((cell, i) => {
const xs: number[] = [], ys: number[] = [];
for (let v = 1; v + 1 < cell.length; v++) {
for (const p of [cell[0], cell[v], cell[v + 1]]) { xs.push(p.x); ys.push(p.y); } // a fan of triangles
}
sciChartSurface.renderableSeries.add(
new FastTriangleRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues: xs, yValues: ys, isSorted: false }),
drawMode: ETriangleSeriesDrawMode.List,
fill: colours[i % colours.length],
opacity: 0.8
}),
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: [...cell, cell[0]].map((p) => p.x),
yValues: [...cell, cell[0]].map((p) => p.y),
isSorted: false
}),
stroke: "#0B1424",
strokeThickness: 2
})
);
});
sciChartSurface.renderableSeries.add(
new XyScatterRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues: sites.map((s) => s.x), yValues: sites.map((s) => s.y), isSorted: false }),
pointMarker: new EllipsePointMarker(wasmContext, { width: 11, height: 11, fill: "#FFFFFF", stroke: "#0B1424" })
})
);
sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier(), new ZoomPanModifier(), new ZoomExtentsModifier());
return { sciChartSurface };
};
}
Key points:
FastTriangleRenderableSerieswithETriangleSeriesDrawMode.Listdraws every three points as one triangle. A convex cell becomes a fan of triangles from its first corner, and one series per colour holds all the cells of that colour.- The cell outlines and the Delaunay edges are each a single
FastLineRenderableSeries, withNaNbetween segments (containsNaN: true) so one series draws many separate lines. - The heatmap modes use
UniformHeatmapRenderableSeries: each grid square holds the colour index of its nearest site, and aHeatmapColorMapwith a sharp step at each whole number turns the indexes into flat colours. - A
CustomChartModifier2Dturns clicks and drags into site edits, and apreRenderhandler keeps one kilometre the same length on both axes, so the bisectors stay at right angles on screen.
See it in the demo app: JavaScript heatmap chart · React heatmap chart · Angular heatmap chart.
Go deeper: SciChart.js triangle series documentation, the uniform heatmap documentation, the scatter series documentation, and the learning center guides to the heatmap, choropleth map and network graph.
Frequently asked questions
What is a Voronoi diagram used for?
A Voronoi diagram answers "which of these places is nearest?" for every point on a map at once. It is used to draw the catchment area of each shop, school, hospital or rain gauge, to model mobile phone coverage, to find nearest neighbours in machine learning, to model grains in metals and cells in tissue, to make cellular textures in computer graphics, and to plan robot paths that stay as far as possible from obstacles.
What is the difference between a Voronoi diagram and a Delaunay triangulation?
They are two views of the same structure. The Voronoi diagram splits the plane into one cell per site. The Delaunay triangulation joins two sites with a line whenever their cells share an edge, which divides the space between the sites into triangles. Each Voronoi vertex is the centre of the circle through the three corners of one Delaunay triangle, and that circle has no other site inside it. Build either one and you can read the other off it.
What are Thiessen polygons?
Thiessen polygons are Voronoi cells under another name, used in geography, hydrology and GIS software. They are named after the meteorologist Alfred Thiessen, who in 1911 drew one around each rain gauge and weighted each gauge's rainfall by its polygon's area to estimate the average rainfall over a region.
How do you draw a Voronoi diagram by hand?
Join each site to its nearby sites with light lines, then draw the perpendicular bisector of each of those lines: the line through its midpoint at a right angle. Around each site, keep only the parts of the bisectors that are closer to that site than to any other. The bisectors meet in threes at the Voronoi vertices, and the pieces between the vertices are the cell edges. Starting from the Delaunay triangles makes it easier, because each vertex is the centre of a triangle's circumcircle.
Are Voronoi cells always convex?
With ordinary straight-line (Euclidean) distance, yes. Each cell is the overlap of half-planes, one per other site, and an overlap of half-planes is always convex. With other distances the cells change shape: Manhattan cells are not always convex, and cells measured along a road network follow the streets.
How do I make a Voronoi diagram in JavaScript?
For a few hundred sites, compute each cell by half-plane clipping: start with a large rectangle and, for every other site, cut away the half beyond the perpendicular bisector. Then cut each cell into triangles from its first corner and draw them, with the cell outlines as a line series and the sites as scatter markers. For other distance measures, colour a grid by its nearest site and draw it as a heatmap. SciChart.js does both with FastTriangleRenderableSeries and UniformHeatmapRenderableSeries, in plain JavaScript, React or Angular.
Sources
- Aurenhammer, F. (1991), Voronoi diagrams: a survey of a fundamental geometric data structure. ACM Computing Surveys, 23(3), 345–405. Properties, the Delaunay dual, algorithms and history.
- de Berg, M., Cheong, O., van Kreveld, M. and Overmars, M. (2008), Computational Geometry: Algorithms and Applications, 3rd edition, Springer. Chapter 7 (Voronoi diagrams, Fortune's algorithm, the and bounds) and chapter 9 (Delaunay triangulations and the angle property).
- Fortune, S. (1987), A sweepline algorithm for Voronoi diagrams. Algorithmica, 2, 153–174.
- Okabe, A., Boots, B., Sugihara, K. and Chiu, S. N. (2000), Spatial Tessellations: Concepts and Applications of Voronoi Diagrams, 2nd edition, Wiley. Applications in geography, biology and materials, and generalised distances.
- Thiessen, A. H. (1911), Precipitation averages for large areas. Monthly Weather Review, 39(7), 1082–1089.
- Snow, J. (1855), map of the Broad Street outbreak with the boundary of equal walking distance to the pump, from the Report on the Cholera Outbreak in the Parish of St. James, Westminster (Cholera Inquiry Committee). John Snow Archive, Michigan State University.
- Hoff, K. E., Keyser, J., Lin, M., Manocha, D. and Culver, T. (1999), Fast computation of generalized Voronoi diagrams using graphics hardware. Proceedings of SIGGRAPH 99, 277–286.
- Worley, S. (1996), A cellular texture basis function. Proceedings of SIGGRAPH 96, 291–294.
- Du, Q., Faber, V. and Gunzburger, M. (1999), Centroidal Voronoi tessellations: applications and algorithms. SIAM Review, 41(4), 637–676.