Here’s the finished, working effect:
int numVertices=int(random(4, 10));
float[] curtX;
float[] curtY;
float[] nextX;
float[] nextY;
float[] angles;
void setup() {
size(600,600);
curtX = new float[numVertices];
curtY = new float[numVertices];
nextX = new float[numVertices];
nextY = new float[numVertices];
angles = new float[numVertices];
// Pick initial random angles.
for (int i=0; i<numVertices; i++) {
angles[i] = random(TWO_PI);
}
// Sort the angles.
angles = sort(angles);
}
void draw() {
background(0);
stroke(255);
noFill();
translate(width/2, height/2);
float radius=280;
ellipse(0,0,2*radius, 2*radius);
// Convert the random angles to (x,y) positions.
for (int i = 0; i < numVertices; i++) {
curtX[i] = cos(angles[i]) * radius;
curtY[i] = sin(angles[i]) * radius;
}
for( int t = 0; t < 20; t++){
draw_polygon();
next_gen();
copy_back();
}
noLoop();
}
void draw_polygon() {
beginShape();
for (int i = 0; i<numVertices; i++) {
vertex(curtX[i], curtY[i]);
}
endShape(CLOSE);
}
void next_gen(){
for( int i = 0; i < numVertices; i++){
nextX[i] = ( curtX[i] + curtX[(i+1)%numVertices] ) / 2.0;
nextY[i] = ( curtY[i] + curtY[(i+1)%numVertices] ) / 2.0;
}
}
void copy_back(){
for( int i = 0; i < numVertices; i++){
curtX[i] = nextX[i];
curtY[i] = nextY[i];
}
}
Make sure you study this - line by line, function by function, one loop at a time - in detail so you understand how it works.