function fillSquares(rows) {
const cells = [{ x: 0, y: 0, x1: rows, y1: rows }];
fill(0);
function fill(i) {
const cell = cells[i];
const point = pick(cell);
const subcells = divide(cell, point).filter(defined);
cells.splice(i, 1, ...subcells);
for (let j = subcells.length - 1; j >= 0; j--) {
if (!square(subcells[j])) fill(i + j);
}
}
function divide(cell, point) {
const { x, y, x1, y1 } = cell;
const [px, py] = point;
return [
{ x, y, x1: px, y1: py },
{ x: px, y, x1: x1, y1: py },
{ x: px, y: py, x1, y1 },
{ x: x, y: py, x1: px, y1: y1 }
];
}
function defined({ x, y, x1, y1 }) {
return x1 - x >= 1 && y1 - y >= 1;
}
function square({ x, y, x1, y1 }) {
return x1 - x === y1 - y && x1 - x !== rows;
}
function pick(cell) {
const { x, y, x1, y1 } = cell;
const points = d3
.cross(d3.range(x, x1 + 1), d3.range(y, y1 + 1))
.filter((point) => divide(cell, point).some(square));
return points[d3.randomInt(points.length)()];
}
return cells;
}