/* ======================================================
Aurthor: Farbod Fathalipouri, Xiaozhou ZHOU
Student ID: 373457, 347214
Natural System Studio, University of Melbourne
NO COPYRIGHT!
welcome to copy, modify and distribute any way you want!
====================================================== */
//============= GLOBAL VARIABLES =============
ArrayList CircleCollection;
//============= SETUP =============
void setup() {
size(800, 600);
smooth();
frameRate(30);
CircleCollection = new ArrayList();
}
//============= DRAW =============
void draw() {
background(0);
Circle temp = new Circle(0,0);
//loop through all circles in the circlecollection arraylist
for (int i = 0; i < CircleCollection.size(); i++) {
temp = (Circle) CircleCollection.get(i);
temp.run(i);
}
}
//============= FUNCTIONS =============
void mousePressed() {
Circle temp = new Circle(mouseX,mouseY);
CircleCollection.add(temp);
}
void keyPressed() {
if (key == 32) { //reset the array list
CircleCollection = new ArrayList();
} else if (key == 8 || key == 127) { //remove the most recent circle by pressing delete or back space
CircleCollection.remove(CircleCollection.size()-1);
}
}
// ===================OBJECT
class Circle {
PVector pos;
float r;
//==================== Constructor, initialize the variables
Circle(float tempX,float tempY){
// create new vector storing information about position
pos = new PVector(tempX,tempY);
r = random(30,height/8);
}
//==================== Methods and Functions
void run(int i){
display();
drawLines(i);
}
// draw circles
void display (){
ellipseMode(CENTER);
noStroke();
fill(200,150);
ellipse(pos.x, pos.y, r*2, r*2);
}
//==========draw line function would find all the circles intersecting and draw line to their centers
void drawLines(int i) { // passing the i value to fuctions for optimizing the algorithm
// check the current circle with the ones after it, in order not to check many times
Circle temp = new Circle(0,0);
for (int j = i+1; j < CircleCollection.size(); j++) {
temp = (Circle) CircleCollection.get(j);
if (checkDistance(temp)) {
stroke(255);
strokeWeight(3);
line(pos.x,pos.y,temp.pos.x,temp.pos.y); //draw lines if two circle intersecting
}
}
}
//find the distance from this circle to the other circle
boolean checkDistance (Circle temp){
float distance = PVector.dist(pos, temp.pos);
if (distance <= (r + temp.r) && distance > 0){ //if distance < sum of two radius and !=0, return true
return true;
}else{
return false;
}
}
}
//draw a line between the circles if they are within some set distance
//recursive function call to find the next closest circle from this circle
//we have just found...
//define a stopping condition to finish the loop
[week4 exercise]
LEFT CLICK - add a circle
DELETE/ BACK SPACE - remove the most recent circle
SPACE - reset