xxxxxxxxxx
function setup() {
createCanvas(400, 400);
const rectWidth = width / 20;
const rectHeight = height / 20;
const triWidth = width / 20;
const triHeight = height / 20;
// Draw grid of rectangles
for (let i = 0; i < 20; i++) {
for (let j = 0; j < 20; j++) {
const x = j * rectWidth;
const y = i * rectHeight;
const color = getRandomColor();
fill(color);
rect(x, y, rectWidth, rectHeight);
}
}
// Draw grid of triangles
for (let i = 0; i < 20; i++) {
for (let j = 0; j < 20; j++) {
const x = j * triWidth;
const y = i * triHeight;
const color = getRandomColor();
fill(color);
push();
translate(x + triWidth / 2, y + triHeight / 2);
rotate(random(TWO_PI));
drawTriangle(-triWidth / 2, -triHeight / 2, triWidth, triHeight);
pop();
}
}
}
// Helper function to draw a triangle
function drawTriangle(x, y, width, height) {
triangle(x + width / 2, y, x + width, y + height, x, y + height);
}
// Helper function to generate a random color
function getRandomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return color(r, g, b);
}
function keyPressed() {
if (key === 's') {
save("my-sketch.png");
}
}