xxxxxxxxxx
57
/*
generative pattern example 4
4/1/2024
*/
function setup() {
createCanvas(400, 400);
noStroke();
var b = createButton("Generate pattern");
b.mousePressed(pattern);
pattern();
}
function pattern() {
background(220);
var cols = 12;
var rows = 8;
// size of each cell
var w = width / cols;
var h = height / rows;
for (let x = 0; x <= width; x += w) {
for (let y = 0; y <= height; y += h) {
fill(0);
// circle(x, y, 5);
stroke(0);
// line(x, y, x + w, y + h);
// line(x + w, y, x, y + h);
push();
translate(x, y);
// rotate(random(PI));
var shapeType = round(random(1, 3));
if (shapeType == 1) {
// cross
line(0, 0, w, h);
line(w, 0, 0, h);
}
if (shapeType == 2) {
line(0, h / 2, w, h / 2);
}
if (shapeType == 3) {
line(w / 2, 0, w / 2, y);
}
pop();
}
}
}