Last active
August 29, 2015 14:24
-
-
Save danbernier/43daf76bed84dcfb580d to your computer and use it in GitHub Desktop.
2D flying circles
This file contains hidden or 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
void setup() { | |
size(800, 600); | |
ballArray = new Ball[40]; | |
for (int i = 0; i < ballArray.length; i++) { | |
ballArray[i] = | |
new Ball(random(width), random(height)); | |
} | |
} | |
Ball[] ballArray; | |
void draw() { | |
background(0); | |
// fill(0, 20); | |
// rect(0, 0, width, height); | |
for (Ball ball : ballArray) { | |
ball.update(); | |
ball.draw(); | |
} | |
stroke(255, 150); | |
float sw = map(mouseY, 0, height, 30, 1); | |
strokeWeight(sw); | |
float minDistance = | |
map(mouseX, 0, width, 0, 250); | |
for (int i = 0; i < ballArray.length; i++) { | |
Ball a = ballArray[i]; | |
for (int j = i; j < ballArray.length; j++) { | |
Ball b = ballArray[j]; | |
if (dist(a.x, a.y, b.x, b.y) < minDistance) { | |
line(a.x, a.y, b.x, b.y); | |
} | |
} | |
} | |
} | |
class Ball { | |
float x; | |
float y; | |
float hue; | |
float dx; | |
float dy; | |
float speed = 4; | |
Ball(float x, float y) { | |
this.x = x; | |
this.y = y; | |
this.hue = random(255); | |
float theta = random(TWO_PI); | |
dx = cos(theta) * speed; | |
dy = sin(theta) * speed; | |
} | |
void update() { | |
x += dx; | |
y += dy; | |
if (x < 0 || x > width) { | |
dx = -dx; | |
} | |
if (y < 0 || y > height) { | |
dy = -dy; | |
} | |
} | |
void draw() { | |
pushStyle(); | |
strokeWeight(1); | |
stroke(255); | |
colorMode(HSB); | |
fill(hue, 255, 255); | |
ellipse(x, y, 50, 50); | |
popStyle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment