Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Created September 8, 2014 02:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save riebschlager/05350ceaa3d1fe96a416 to your computer and use it in GitHub Desktop.
Save riebschlager/05350ceaa3d1fe96a416 to your computer and use it in GitHub Desktop.
ArrayList<Pathfinder> pathsLeft = new ArrayList<Pathfinder>();
ArrayList<Pathfinder> pathsRight = 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();
pathsLeft.add(new Pathfinder(-1));
pathsRight.add(new Pathfinder(1));
center = new PVector(canvas.width / 2, canvas.height / 2);
}
void draw() {
canvas.beginDraw();
for (int i=0; i < pathsLeft.size (); i++) {
if (!pathsLeft.get(i).isDead) {
Pathfinder p = pathsLeft.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);
p.update();
}
}
for (int i=0; i < pathsRight.size (); i++) {
if (!pathsRight.get(i).isDead) {
Pathfinder p = pathsRight.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);
p.update();
}
}
canvas.endDraw();
image(canvas, 0, 0, width, height);
}
void keyPressed() {
if (key == ' ') {
canvas.beginDraw();
canvas.background(255);
canvas.endDraw();
pathsLeft.clear();
pathsLeft.add(new Pathfinder(-1));
pathsRight.clear();
pathsRight.add(new Pathfinder(1));
}
if (key == 's') {
String filename = "output/" + random(10000) + ".tif";
canvas.save(filename);
}
}
class Pathfinder {
PVector location;
PVector velocity;
float diameter;
int initDir;
boolean isDead = false;
Pathfinder(int _dir) {
location = new PVector(canvas.width / 2, canvas.height / 2);
velocity = new PVector(_dir, 0);
diameter = 20;
initDir = _dir;
}
Pathfinder(Pathfinder parent) {
location = parent.location.get();
velocity = parent.velocity.get();
initDir = parent.initDir;
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.08);
velocity.add(bump);
velocity.normalize();
if (random(0, 1) < 0.01) {
if (initDir == 1) {
pathsLeft.add(new Pathfinder(this));
}
if (initDir == -1) {
pathsRight.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