Hi all, I am working on this animation which is triggered by the sound input, every time a certain amplitude is picked up a circle is drawn and it makes the animation start. Now I'd like to make the sketch restart (with nothing being drawn) once the maximum count of circles is reached. Is there a way to do it ?
//The generative design is a variation of the programs presented in the
//Generative Design book by Hartmut Bohnacker, Benedikt Gross, Julia Laub and Claudius Lazzeroni.
//Starting point: Every time an audio amplitude > of a certain value is detected,
//a new circle is generated at a random position and with a radius determined by the amplitude.
//It is then determined which of the existing circles lies nearest to the new one.
//In the final step, the new circle joins its closest neighbor via the shortest path.
//This is an example of so-called diffusion limited aggregation algorithm.
import processing.sound.*;
int maxCount = 10000; //max count of the cirlces
int currentCount = 1;
float[] x = new float[maxCount];
float[] y = new float[maxCount];
float[] r = new float[maxCount]; // radius
Amplitude amp;
AudioIn in;
float ampt;
void setup() {
fullScreen();
// size(600,600);
smooth();
frameRate(1000);
// first circle
x[0] = width/2;
y[0] = height/2;
r[0] = ampt * 10;
//r[0] = 400;
amp = new Amplitude(this);
in = new AudioIn(this, 0);
in.start();
amp.input(in);
}
void draw() {
ampt = amp.analyze();
println(ampt);
if (ampt>0.03) { // everytime a sound amplitude bigger than 0.03 is detected, a new ellipse is drawn
strokeWeight(0.5);
//noFill();
background(255);
// create a radom set of parameters
float newR = ampt*10; // amplitude controls the radius of the circles being drawn
float newX = random(0+newR, width-newR);
float newY = random(0+newR, height-newR);
float closestDist = 100000000;
int closestIndex = 0;
// which circle is the closest?
for(int i=0; i < currentCount; i++) {
float newDist = dist(newX,newY, x[i],y[i]);
if (newDist < closestDist) {
closestDist = newDist;
closestIndex = i;
}
}
// aline it to the closest circle outline
float angle = atan2(newY-y[closestIndex], newX-x[closestIndex]);
x[currentCount] = x[closestIndex] + cos(angle) * (r[closestIndex]+newR);
y[currentCount] = y[closestIndex] + sin(angle) * (r[closestIndex]+newR);
r[currentCount] = newR;
currentCount++;
// draw them
for (int i=0 ; i < currentCount; i++) {
//fill(50,150);
fill (0);
//fill(ampt*500, ampt*200, ampt*200, ampt*300);
ellipse(x[i],y[i], r[i]*2,r[i]*2);
}
if (currentCount >= maxCount) noLoop();
}
}