Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Created September 8, 2014 02:23
Show Gist options
  • Save riebschlager/79d81399fafbeb9cf43e to your computer and use it in GitHub Desktop.
Save riebschlager/79d81399fafbeb9cf43e to your computer and use it in GitHub Desktop.
ArrayList<Pathfinder> paths = new ArrayList<Pathfinder>();
PVector center;
PGraphics canvas;
void setup() {
size(displayWidth, displayHeight);
canvas = createGraphics(displayWidth * 2, displayHeight * 2);
canvas.beginDraw();
canvas.background(255);
canvas.ellipseMode(CENTER);
canvas.fill(0, 100, 100);
canvas.noStroke();
canvas.colorMode(HSB);
canvas.endDraw();
paths.add(new Pathfinder());
center = new PVector(canvas.width / 2, canvas.height / 2);
}
void draw() {
canvas.beginDraw();
for (int i=0; i < paths.size (); i++) {
if (!paths.get(i).isDead) {
Pathfinder p = paths.get(i);
float dist = dist(p.location.x, p.location.y, center.x, center.y);
float B = map(dist, 0, canvas.height/2, 0, 255);
canvas.fill(0, 250, B);
canvas.ellipse(p.location.x, p.location.y, p.diameter, p.diameter);
canvas.pushMatrix();
canvas.scale(-1,1);
canvas.ellipse(p.location.x - canvas.width, p.location.y, p.diameter, p.diameter);
canvas.popMatrix();
p.update();
}
}
canvas.endDraw();
image(canvas, 0, 0, width, height);
}
void keyPressed() {
if (key == ' ') {
canvas.beginDraw();
canvas.background(255);
canvas.endDraw();
paths.clear();
paths.add(new Pathfinder());
}
if (key == 's') {
String filename = "output/" + random(10000) + ".tif";
canvas.save(filename);
}
}
class Pathfinder {
PVector location;
PVector velocity;
float diameter;
boolean isDead = false;
Pathfinder() {
location = new PVector(canvas.width / 2, canvas.height / 2);
velocity = new PVector(-1, 0);
diameter = 40;
}
Pathfinder(Pathfinder parent) {
location = parent.location.get();
velocity = parent.velocity.get();
float area = PI * sq(parent.diameter / 2);
float newDiam = sqrt(area / 2 / PI) * 2;
diameter = newDiam;
parent.diameter = newDiam;
}
void update() {
if (diameter > 0.05) {
location.add(velocity);
PVector bump = new PVector(random(-1, 1), random(-1, 1));
bump.mult(0.0875);
velocity.add(bump);
velocity.normalize();
if (random(1) > 0.99) {
paths.add(new Pathfinder(this));
}
} else {
isDead = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment