xxxxxxxxxx
88
/*
Inspired by works from Frieder Nake's Matrix Multiplication Series
*/
//define variables
let cellSize;
let sign;
//set seed to 2
let seed = 2;
//initialize setup
function setup() {
//make canvas according to window width and height
createCanvas(windowWidth, windowHeight);
//create rectangles from their center
rectMode(CENTER);
//set angle definitions to degrees
angleMode(DEGREES);
//don't fill shapes
noFill();
//black outline
stroke(0);
//width of outline = 2
strokeWeight(2);
}
//start draw loop
function draw() {
//set background to white
background(255);
//choose a random seed
randomSeed(seed);
//set cellsize to a either 10% of width or height, depending on which is smaller
cellSize = min(width / 10, height / 10);
//start for loop, define x to half of cellSize, add cellSize until x reaches width
for (let x = cellSize / 2; x < width; x += cellSize) {
//start for loop, define y to half of cellSize, add cellSize until y reaches height
for (let y = cellSize / 2; y < height; y += cellSize) {
//start transformation
push();
//move shape to x, y coordinates
translate(x, y);
//define chance to a random number between 0 and 1
let chance = random(1);
//if chance is less than 0.5, set sign to 1
if (chance < 0.5) {
sign = 1;
//else set sign to -1
} else {
sign = -1;
}
//set angle to a random number between 0 to 60 or 0 to -60 depending on sign value
let angle = random(0, -60 * sign);
//rotate by angle degree
rotate(angle);
//if angle is less than 0, then resize the square by a random amount between 0.5 to 1.
if (angle < 0) {
square(0, 0, cellSize * random(0.5, 1));
//otherwise keep it the same size
} else {
//create square with variables
square(0, 0, cellSize);
}
//end transformation
pop();
}
}
}
//randomize seed when mouse is pressed
function mousePressed() {
seed = random(50000);
}
//resize canvas when the window is resized
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}