import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class BifurkationMB extends PApplet {// Max Bense
// Bifurkation
// 2006 Peter Chiochetti
// for Thomas Feuerstein

int maxLen = 32;
int c;
int black = color(0);
int white = color(255);
Vector nodes;

public void setup() {
  size (768, 576);
  background(208);
  strokeCap(PROJECT);
  frameRate(25);
  nodes = new Vector();
  nodes.add(new Node(width/2, height/2, black));
  c = 1;
}

public void draw() {
  strokeWeight(32);
  c++;
  int n = nodes.size();
  ((Node)nodes.get(PApplet.parseInt(random(n)))).grow();
  if (n >= 1000)
    nodes.removeElementAt(n - 2);
}

class Node {
  float x, y;
  int c;

  Node(float a, float b, int z) {
    x = a;
    y = b;
    c = z;
  }

  public void grow() {
    float ox = x;
    float oy = y;
    float dx = maxLen;
    float dy = maxLen;
    if (random(2) >= 1)
      dx = 0;
    else
      dy = 0;

    x += dx;
    y += dy;
    if (x < 0 || y < 0 || x > width || y > height)
    {
      nodes.removeElement(this);
      return;
    }
    stroke(color(random(10) * 25));
    if (c == black)
      stroke (white);
    line(ox, oy, x, y);

    float x1 = ox - dx;
    float y1 = oy - dy;
    if (dx < dy || x1 > x || y1 > y)
      x1 = ox + dx;
    else
      y1 = oy + dy;
    if (x1 < 0 || y1 < 0 || x1 > width || y1 > height)
      return;
    nodes.add(new Node(x1, y1, white));
    stroke(color(random(10) * 25));
    if (c == black)
      stroke (black);
    line(ox, oy, x1, y1);
  }
}
static public void main(String args[]) {   PApplet.main(new String[] { "BifurkationMB" });}}