xxxxxxxxxx
function setup() {
createCanvas(400, 600);
noLoop();
stroke(0);
strokeWeight(5);
}
function randomColor() {
return color(random(255), random(255), random(255));
}
function drawRects(x, y, w, h, depth=0) {
if (depth > 4) return;
let isHorizontal = random() > 0.5;
if (isHorizontal) {
let splitY = y + random(0.2, 0.8) * h;
fill(randomColor());
rect(x, y, w, splitY - y);
fill(randomColor());
rect(x, splitY, w, y + h - splitY);
drawRects(x, y, w, splitY - y, depth+1);
drawRects(x, splitY, w, y + h - splitY, depth+1);
} else {
let splitX = x + random(0.2, 0.8) * w;
fill(randomColor());
rect(x, y, splitX - x, h);
fill(randomColor());
rect(splitX, y, x + w - splitX, h);
drawRects(x, y, splitX - x, h, depth+1);
drawRects(splitX, y, x + w - splitX, h, depth+1);
}
}
function draw() {
background(randomColor());
drawRects(0, 0, width, height);
}