👁 648 | Tristan Schulze lately updated this on Mai 28, 2024
0
basic_triangle_grid with random rotation
int gc = 20; // Number of grid cells
void setup() {
size(600, 600); // Set up the canvas size
background(255); // Set the background color to white
noStroke(); // Disable drawing the stroke (outline)
makeTheAnni(); // Call the function to draw the initial pattern
}
void keyPressed() {
// Check if the 'c' key is pressed
if (key == 'c') {
saveFrame("the_anni_####.png"); // Save the current frame as an image file
println("SAVED! :)"); // Print confirmation message to the console
}
}
void mousePressed() {
// Check if the left mouse button is pressed
if (mouseButton == LEFT) {
makeTheAnni(); // Call the function to create a new pattern
println("VARIANTE"); // Print confirmation message to the console
}
}
void draw() {
// The draw function is empty because we only need to update the canvas
// when the mouse is pressed or a key is pressed.
}
void makeTheAnni() {
background(255); // Reset the background to white
float cell_size = width / float(gc); // Calculate the size of each grid cell
// Loop through each cell in the grid
for (int x = 0; x < gc; x++) {
for (int y = 0; y < gc; y++) {
float cx = x * cell_size; // Calculate the x-coordinate of the cell
float cy = y * cell_size; // Calculate the y-coordinate of the cell
// Save the current transformation matrix
pushMatrix();
// Translate to the center of the cell
translate(cx + cell_size * 0.5, cy + cell_size * 0.5);
// Rotate the triangle by a random multiple of 90 degrees
rotate(round(random(5)) * HALF_PI);
// Draw a triangle in the cell
triangle(
-cell_size * 0.5, -cell_size * 0.5, // First vertex
cell_size * 0.5, -cell_size * 0.5, // Second vertex
-cell_size * 0.5, cell_size * 0.5 // Third vertex
);
// Restore the previous transformation matrix
popMatrix();
// Fill the triangle with black color
fill(0);
}
}
}