int NUM=15, // worm length
TOT=16, // # worms
DIM=10; // max speed
Worm[][] W= new Worm[TOT][NUM];
void setup()
{
size(500, 350);
frameRate(30);
noStroke();
smooth();
creaWorm();
}
void creaWorm()
{
color colore;
float raggio;
for(int i=0; i < NUM; i++)
{
raggio=2*i+5;
for(int j=0; j < TOT; j++)
{
colore = color(random(255), random(255), random(255));
W[j][i]=new Worm(width/2, height/2, raggio, colore);
}
}
}
void mousePressed()
{
creaWorm();
}
float d;
void draw()
{
background(0);
for(int j=0; j < TOT; j++)
{
d=random(-DIM, DIM);
if(W[j][0].xpos+d > 0 && W[j][0].xpos+d < width)
W[j][0].xpos+=d;
d=random(-DIM, DIM);
if(W[j][0].ypos+d > 0 && W[j][0].ypos+d < height)
W[j][0].ypos+=d;
}
for(int i=1; i < NUM; i++)
for(int j=0; j < TOT; j++) {
W[j][i].xpos=(W[j][i-1].xpos+W[j][i].xpos)/2;
W[j][i].ypos=(W[j][i-1].ypos+W[j][i].ypos)/2;
}
for(int i=NUM-1; i >= 0; i--)
for(int j=0; j < TOT; j++)
W[j][i].draw();
}
class Worm
{
float xpos, ypos;
float raggio;
color colore;
Worm(float xp, float yp, float ra, color co)
{
xpos=xp; ypos=yp;
raggio=ra; colore=co;
}
void draw()
{
fill(colore);
ellipse(xpos, ypos, raggio, raggio);
}
}