xxxxxxxxxx
// Set the size of each cell in the grid
let cellSize = 30;
// Variables to store the current colors for squares and circles
let currentColor;
function setup() {
// Create a canvas
createCanvas(600, 600);
// Stroke kalınlığı
strokeWeight(2);
noFill();
background(255);
// Initialize the colors with random values
currentColor = color(0);
noLoop();
// Loop through the canvas to draw the pattern in a grid
for (let x = 0; x < width; x += cellSize) {
for (let y = 0; y < height; y += cellSize) {
// Call the drawPattern function for each cell in the grid
drawPattern(x, y, cellSize);
}
}
}
// Function to draw a pattern in a given cell
function drawPattern(x, y, size) {
// Generate a random choice for the pattern (0 to 3)
let choice = floor(random(4));
let rotation = random(TWO_PI); // Random rotation angle
// Push and pop to isolate transformations to this specific cell
push();
// Translate to the center of the cell
translate(x + size / 2, y + size / 2);
rotate(rotation); // Apply random rotation
// Randomly generate a fill color for circles and squares
let fillColor = color(random(255), random(255), random(255));
fill(fillColor);
// Begin a shape that will represent the chosen pattern
beginShape();
switch (choice) {
case 0:
// Diagonal lines
stroke(currentColor);
vertex(-size / 2, -size / 2);
vertex(size / 2, size / 2);
break;
case 1:
// Horizontal lines
stroke(currentColor);
vertex(-size / 2, 0);
vertex(size / 2, 0);
break;
case 2:
// Vertical lines
stroke(currentColor);
vertex(0, -size / 2);
vertex(0, size / 2);
break;
case 3:
// Circles
noStroke();
for (let i = 0; i < 360; i += 10) {
let angle = radians(i);
let xpos = cos(angle) * (size * 0.4);
let ypos = sin(angle) * (size * 0.4);
vertex(xpos, ypos);
}
break;
}
endShape(CLOSE);
pop();
}
//random when press a/A
function keyPressed() {
if (key === "a" || key === "A") {
// Change the current color to a new random color
currentColor = color(random(255), random(255), random(255));
background(255);
// Redraw the pattern with the new color
for (let x = 0; x < width; x += cellSize) {
for (let y = 0; y < height; y += cellSize) {
drawPattern(x, y, cellSize);
}
}
}
//m/M tuşuna bastığımızda ödev indirilsin
else if (key === "m" || key === "M") {
save("homework8.jpeg");
}
}