Last active
August 29, 2015 14:08
-
-
Save qubbit/0bf06540b787afd3a01b to your computer and use it in GitHub Desktop.
Animating Circles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @author: gopal adhikari | |
I saw an animation like this online several months ago, I cannot remember the site, | |
but that's where I got the inspiration from. | |
*/ | |
void setup() { | |
size(800, 600); | |
noStroke(); | |
smooth(); | |
circles = new Circle[MAX_CIRCLES]; | |
for (int i = 0; i < MAX_CIRCLES; i++) { | |
Circle c = new Circle(); | |
c.radius = random(5, 20); | |
c.position = new PVector(random(width), random(height)); | |
c.velocity = new PVector(random(-0.1, 0.1), random(-0.1, 0.1)); | |
c.colour = color(#ffffff); | |
circles[i] = c; | |
} | |
} | |
private final int MAX_CIRCLES = 20; | |
private final float DISTANCE = 125.0; | |
private final boolean SAVING = false; | |
private Circle[] circles; | |
void draw() { | |
background(39, 31, 41); | |
for (int i = 0; i < MAX_CIRCLES; i++) { | |
Circle a = circles[i]; | |
for (int j = 0; j < MAX_CIRCLES; j++) { | |
if (i == j) continue; | |
Circle b = circles[j]; | |
if (PVector.dist(a.position, b.position) < DISTANCE) { | |
strokeWeight(2); | |
stroke(#ffffff, 60); | |
line(a.position.x, a.position.y, b.position.x, b.position.y); | |
} | |
checkCollision(a); | |
a.position.x += a.velocity.x; | |
a.position.y += a.velocity.y; | |
fill(a.colour, 100); | |
ellipse(a.position.x, a.position.y, a.radius, a.radius); | |
} | |
} | |
if (SAVING) saveFrame("f####.gif"); | |
} | |
void checkCollision(Circle c) { | |
if (c.position.x > width || c.position.y > height) { | |
c.velocity.rotate(PI / 2.0); | |
} else if (c.position.x < 0 || c.position.y < 0) { | |
c.velocity.rotate(PI / 2.0); | |
} | |
} | |
public class Circle { | |
PVector position; | |
PVector velocity; | |
float radius; | |
color colour; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment