class Circle {
float x,y; //location
float dia; //diameter
float exSpd; //expansion speed
color c; //color of each circle
Circle (float x_, float y_) {
dia = 10; //starting diameter, also dot for moving, and dot for bounce-back
x = x_; //x position
y = y_; //y position
c = color(random(255), random(255), random(255)); //randomize color of circle
}
void display(float exSpd_) {
exSpd = exSpd_;
stroke(c); //random color
strokeWeight(4);
fill(255);
ellipse(x,y,dia,dia); //displays the circle
dia += exSpd; //expands the circle at rate pre-defined
noStroke();
fill(c);
ellipse(x,y,10,10); //displays a solid circle in the center of expanding one
}
float diameter() { //sends diamter expansion data to the main draw loop for storing in an array
return dia;
}
}
ArrayList circles;
//Circle[] circles = new Circle[0]; //object array
float[] expandSpeed = new float[0]; //speed array
float[] xCoord = new float[0]; //x coordinates array
float[] yCoord = new float[0]; //y coordinates array
float[] diaArray = new float[0]; //diameters array
int iter; //counter to keep track of current interation
float x,y,dia;
void setup() {
size(400,400);
smooth();
circles = new ArrayList(); //create blank arraylist
}
void draw() {
background(255);
for(int i = 0; i < circles.size(); i++) { //loop for the current number of objects
iter = i; //iteration counter equals i
Circle circle = (Circle) circles.get(i); //get object from ArrayList position i
circle.display(expandSpeed[i]); //display the object Array
diaArray[i] = circle.diameter(); //fill the diameter array to that it updates with display
if (circle.diameter() < 13) { //if circle gets too small, expand
expandSpeed[i] = 1;
}
x = xCoord[iter]; //pull the current iteration of x coordinates
y = yCoord[iter]; //pull the current iteration of y coordinates
dia = diaArray[iter]; //pull the current iteration of diameter data
for(int h = 0; h < diaArray.length-1; h++) { //for each indice currently holding object/xy/diameter data
if (h != iter) { //NOT including the iteration that was pulled before this loop
float distance = dist(x,y,xCoord[h],yCoord[h]); //get distance between current iteration and all other points
if(distance < dia/2+diaArray[h]/2) { //if that distance is greater than current radius plus all other radi
expandSpeed[h] = -1; //reverse current iteration
expandSpeed[iter] = -1; //and all other colliding iterations
}
}
}
}
}
void mousePressed() {
float xCoord_ = mouseX; //add place to xCoord array and input current mouseX data
xCoord = (float[]) append(xCoord, xCoord_);
float yCoord_ = mouseY; //add place to yCoord array and input current mouseY data
yCoord = (float[]) append(yCoord, yCoord_);
float diaArray_ = 10; //add place to diameter Array and input dumb placeholder data
diaArray = (float[]) append(diaArray, diaArray_);
float expandSpeed_ = .2; //add place to expnasion speed Array and input the speed
expandSpeed = (float[]) append(expandSpeed, expandSpeed_);
circles.add(new Circle(mouseX,mouseY)); //add new object to object arrayList
}
void keyPressed() {
if (circles.size() > 0) {
circles.remove(circles.size()-1);
xCoord = shorten(xCoord);
yCoord = shorten(yCoord);
diaArray = shorten(diaArray);
expandSpeed = shorten(expandSpeed);
}
}
inspired from the Pulsate App for android phones.
press the anykey to undo last placement.