createVoronoiTessellation = (width, height, pts, iteration = 12) => {
const delaunay = d3.Delaunay.from(
pts,
(d) => d[0],
(d) => d[1]
);
const voronoi = delaunay.voronoi([0, 0, width, height]);
if (iteration === 0) {
const { polygons } = getCentroidAndCellsFromVoronoi(voronoi);
return { points: pts, polygons };
}
for (let k = 0; k < iteration; k++) {
for (let i = 0; i < delaunay.points.length; i += 2) {
const cell = voronoi.cellPolygon(i >> 1);
if (cell === null) continue;
const x0 = delaunay.points[i];
const y0 = delaunay.points[i + 1];
const [x1, y1] = d3.polygonCentroid(cell);
delaunay.points[i] = x0 + (x1 - x0) * 1;
delaunay.points[i + 1] = y0 + (y1 - y0) * 1;
}
voronoi.update();
}
return getCentroidAndCellsFromVoronoi(voronoi);
}