The example above was creating an array of 64 shapes. If you only need one, you don’t need that array, and you don’t need the for loop to iterate 64 times creating and drawing all shapes. You just need one I assume.
You can keep the polygon function, which just returns a shape with a polygon. The following example creates and draws a polygon every time you press a key:
void setup() {
size(600, 600, P2D);
background(255);
}
void draw() {
}
void keyPressed() {
noStroke();
fill(random(255));
PShape shp = polygon(200, (int)random(3, 10));
translate(width * 0.5, height * 0.5); // move to the center of the screen
rotate(random(TWO_PI)); // random rotation
shape(shp); // draw the polygon
}
PShape polygon(float radius, int npoints) {
// create a random list of angles
float angles[] = new float[npoints];
for (int i=0; i<npoints; i++) {
angles[i] = random(TWO_PI);
}
// sort the list
angles = sort(angles);
// create a shape with those angles
PShape s = createShape();
s.beginShape();
for (float a : angles) {
float sx = cos(a) * radius;
float sy = sin(a) * radius;
s.vertex(sx, sy);
}
s.endShape(CLOSE);
return s;
}
Notice that it assigns a random gray color to each polygon, this way you can notice the overlapping polygons. But of course you can change this.
Also notice that the polygon function takes two arguments: first, the radius of the polygon, and second, the number of points it should have.