xxxxxxxxxx
function setup() {
createCanvas(600, 600); // Set up a 600x600 canvas
background(255); // White background
drawGrid(); // Call function to draw the board
}
function drawGrid() {
background(255); // Reset background to white
var rows = 5; // Number of rows
var columns = 5; // Number of columns
var cellW = width / columns; // Width of each cell
var cellH = height / rows; // Height of each cell
var pad = 3; // Small padding inside each cell
// Loop over each cell in the grid
for (var col = 0; col < columns; col++) {
for (var row = 0; row < rows; row++) {
let x = col * cellW; // X-coordinate of cell
let y = row * cellH; // Y-coordinate of cell
fill(0); // Black cell background
rect(x, y, cellW, cellH); // Draw cell rectangle
let flippedN = random() < 0.1; // 10% chance to flip "N" shape
fill(255); // White for "N" shapes
noStroke(); // No outline
if (flippedN) {
// Draw mirrored "N" shape
rect(x + pad, y + pad, cellW / 5, cellH - 2 * pad); // Right vertical bar
rect(x + cellW - pad - cellW / 5, y + pad, cellW / 5, cellH - 2 * pad); // Left vertical bar
// Diagonal line from top-right to bottom-left
beginShape();
vertex(x + cellW - pad, y + pad); // Top-right
vertex(x + cellW - pad - cellW / 5, y + pad); // Inner top-right
vertex(x + pad, y + cellH - pad); // Bottom-left
vertex(x + pad + cellW / 5, y + cellH - pad); // Inner bottom-left
endShape(CLOSE); // Complete mirrored "N"
} else {
// Draw regular "N" shape
rect(x + pad, y + pad, cellW / 5, cellH - 2 * pad); // Left vertical bar
rect(x + cellW - pad - cellW / 5, y + pad, cellW / 5, cellH - 2 * pad); // Right vertical bar
// Diagonal line from top-left to bottom-right
beginShape();
vertex(x + pad, y + pad); // Top-left
vertex(x + pad + cellW / 5, y + pad); // Inner top-left
vertex(x + cellW - pad, y + cellH - pad); // Bottom-right
vertex(x + cellW - pad - cellW / 5, y + cellH - pad); // Inner bottom-right
endShape(CLOSE); // Complete regular "N"
}
}
}
}
function keyPressed() {
if (key == 'r' || key == 'R') {
drawGrid(); // Redraw board with new random "N"s on 'R' key press
} else if (key == 's' || key == 'S') {
saveCanvas('N_pattern', 'png'); // Save image as PNG on 'S' key press
}
}