// Two ball variables Ball ball1; Ball ball2; Ball ball3; Ball ball4; Ball ball5; void setup() { size(400,400); framerate(50); smooth(); // Initialize balls ball1 = new Ball(64); ball2 = new Ball(32); ball3 = new Ball(48); ball4 = new Ball(85); ball5 = new Ball(18); } void draw() { background(255,0,0); //Move and display balls ball1.move(); ball2.move(); ball3.move(); ball4.move(); ball5.move(); boolean intersecting = ball1.intersect(ball2); ball2.intersect(ball3); ball3.intersect(ball1); ball4.intersect(ball2); ball4.intersect(ball3); ball5.intersect(ball1); ball5.intersect(ball2); ball5.intersect(ball3); ball5.intersect(ball4); ball4.intersect(ball1); if (intersecting) { ball1.highlight(); ball2.highlight(); ball3.highlight(); ball4.highlight(); ball5.highlight(); } ball1.display(); ball2.display(); ball3.display(); ball4.display(); ball5.display(); } class Ball { float r; // radius float x,y; float xspeed,yspeed; color c = color(255,50); // Constructor Ball(float r_) { r = r_; x = random(width); y = random(height); xspeed = random(-5,5); yspeed = random(-5,5); } void move() { x += xspeed; // Increment x y += yspeed; // Increment y // Check horizontal edges if (x > width || x < 0) { xspeed *= -1; } // Check vertical edges if (y > height || y < 0) { yspeed *= -1; } } // Draw the ball void display() { stroke(255); fill(c); ellipse(x,y,r*2,r*2); c = color(255,50); } void highlight() { c = color(255,150); } // A function that returns true or false based on whether two circles intersect // If distance is less than the sum of radii the circles touch boolean intersect(Ball b) { float distance = dist(x,y,b.x,b.y); // Calculate distance if (distance < r + b.r) { return true; } else { return false; } } }