xxxxxxxxxx
50
/*
Based on Don Karlsson's Recursive Squares contribution to CodingTrain Community
References:
https://thecodingtrain.com/CodingChallenges/077-recursion.html
https://codepen.io/DonKarlssonSan/pen/PJQvKG
*/
let angle = 0;
let iterations = 5;
function setup() {
createCanvas(windowWidth, windowHeight);
rectMode(CORNER);
strokeWeight(2);
noStroke();
fill(255, 125);
}
function draw() {
background(0);
angle += 0.001;
let x = width / 2;
let y = height / 2;
drawSquare(iterations, x, y);
}
function drawSquare(iteration, x, y) {
if (iteration > 0) {
iteration--;
push();
translate(x, y);
rotate(iteration * angle);
let windowMin = min(width, height);
let size = ((iteration / iterations) * windowMin) / 3;
square(-size / 2, -size / 2, size);
drawSquare(iteration, -size / 2, -size / 2);
drawSquare(iteration, size / 2, size / 2);
drawSquare(iteration, size / 2, -size / 2);
drawSquare(iteration, -size / 2, size / 2);
pop();
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
background(0);
}