xxxxxxxxxx
52
let angle = 0; // Global variable to track spiral angle
let radius = 50; // Initial radius for the spiral
function setup() {
createCanvas(400, 400);
background('black');
}
function draw() {
// Call the Spiral function when mouse is pressed
if (mouseIsPressed){
Spiral();
}
}
// Function to draw shapes with a random number of sides
function DrawShape(x,y,radius,numsides){
beginShape();
// reference from https://www.gorillasun.de/blog/a-guide-to-hexagonal-grids-in-p5js/
for ( a = 0 ; a < TWO_PI ; a += TWO_PI / numsides ){
let xpos = x + cos(a) * radius;
let ypos = y + sin(a) * radius;
vertex(xpos,ypos);
}
endShape(CLOSE);
}
// Function to create a spiral of random shapes
function Spiral(){
// Calculate the x and y position using polar coordinates
let x = width / 2 + radius * cos(angle);
let y = height / 2 + radius * sin(angle);
// Randomize the number of sides for the shape (between 3 and 7)
let numsides = floor(random(3, 8));
// Randomize the color of the shape
let shapeColor = color(random(255),random(255),random(255));
fill(shapeColor);
// Draw the shape at the calculated position
DrawShape(x, y, 25, numsides);
// Update angle and radius for the next shape in the spiral
angle += 0.2;
radius -= 2;
}