lloydAlgo = (points, NumIterations = 20) => {
const voronoi = d3.Delaunay.from(points, d => d.x, d => d.y).voronoi([
0.5,
0.5,
width - 0.5,
height - 0.5
]);
for (let i = 0; i < NumIterations; ++i) {
for (let j = 0; j < points.length; ++j) {
const cell = voronoi.cellPolygon(j);
const [x_centroid, y_centroid] = d3.polygonCentroid(cell);
voronoi.delaunay.points[j * 2] +=
(x_centroid - voronoi.delaunay.points[j * 2]) * 0.03;
voronoi.delaunay.points[j * 2 + 1] +=
(y_centroid - voronoi.delaunay.points[j * 2 + 1]) * 0.03;
}
voronoi.update()
}
return Array.from(points, (_, j) => ({
x: voronoi.delaunay.points[2 * j],
y: voronoi.delaunay.points[2 * j + 1]
}));
}