xxxxxxxxxx
47
/*
----- Coding Tutorial by Patt Vira -----
Name: Recursive Pattern #3
Video Tutorial: https://youtu.be/Lg7dgUhQyPc
Connect with Patt: @pattvira
https://www.pattvira.com/
----------------------------------------
*/
let x, y, w, h;
let direction;
function setup() {
createCanvas(400, 400);
x = 0; y = 0;
w = width; h = height;
direction = floor(random(2));
}
function draw() {
background(220);
divide(x, y, w, h, 0, direction);
noLoop();
}
function divide(x, y, w, h, count) {
let tiles = [];
let num = 3;
let size = w / num;
for (let i=0; i<num; i++) {
for (let j=0; j<num; j++) {
tiles.push(new Tile(x + i*size, y + j*size, size, size));
}
}
for (let i=0; i<tiles.length; i++) {
if (count < 2 && random() < 0.5) {
divide(tiles[i].x, tiles[i].y, tiles[i].w, tiles[i].h, count + 1);
} else {
tiles[i].display();
}
}
}