getRandomGrid = (
width,
height,
step = 10,
amount = Math.round((width * height) / step ** 2),
rndseed = 1984
) => {
amount = amount >= 0 ? amount : Math.round((width * height) / step ** 2);
const rnd = almostRandom(rndseed);
const start = new Array(amount)
.fill(0)
.map(() => [rnd() * width, rnd() * height]);
const delaunay = Delaunay.from(start);
const voronoi = delaunay.voronoi([0, 0, width, height]);
const points = [];
for (let k = 0; k < relax; 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] = polygonCentroid(cell);
delaunay.points[i] = x1;
delaunay.points[i + 1] = y1;
}
voronoi.update();
}
for (let i = 0; i < delaunay.points.length; i += 2) {
const c = colors[i%colors.length]
points.push([delaunay.points[i], delaunay.points[i + 1], c]);
}
return points;
}