function drawPolygon(points, iterations, context) {
if (iterations === 0) return;
const color = stepColor(iterations);
const lines = [];
for (let [i, p] of points) {
const [_, q] = points[(i + 1) % points.length];
const l = line(p, q, context, {
lineWidth: 1,
color,
lineWidth: 1.5,
dash: [1, 0]
});
lines.push([i, l]);
}
let nextPoints = [];
for (let [i, l] of lines) {
const p = pointOnLine(l, 0.5);
circle([...p, 1], context);
nextPoints.push([i, p]);
}
return drawPolygon(nextPoints, iterations - 1, context);
}