ashlee3dee_Processing/BSP/BSP.pde
boolean recording = false;        // A boolean to track whether we are recording are not
float timeScale = 0.0005f;        //global amount to scale millis() for all animations
boolean redraw = true;
float currentTime = 0;
int noderes = 4;

BSPNode[] bspnodes = new BSPNode[noderes*noderes];


void settings()
{
  size(800, 800, P3D);
}

void setup() {
  frameRate(30);                  //all code should be frame-rate independent. hint: use millis()
  smooth(2);                      //enable highest level of anti-aliasing your system can handle
  ellipseMode(CENTER);
  rectMode(CENTER);
  imageMode(CENTER);
  blendMode(NORMAL);
  colorMode(HSB);
  background(0, 0, 0);
  noiseSeed((long)random(2^32));
  for (int ix=0; ix < noderes; ix++) {
    for (int iy=0; iy < noderes; iy++) {
      BSPNode b = new BSPNode((int)random(8), width/(noderes+1));
      bspnodes[noderes * iy + ix] = b;
    }
  }
}

void draw() {
  tick();  //no touch
  if (redraw) {
    background(0, 0, 0);
    //println(floor(frameCount%frameRate));
    //bsp.reset();
    //bsp.split();
    for (int ix=0; ix < noderes; ix++) {
      for (int iy=0; iy < noderes; iy++) {
        pushMatrix();
        pushStyle();
        translate((ix+0.5)*(width/noderes), (iy+0.5)*(height/noderes));
        PVector s = bspnodes[noderes * iy + ix].size;
        translate(s.x*2, s.y*2);
        //bspnodes[noderes * iy + ix].draw();
        popStyle();
        bspnodes[noderes * iy + ix].visualB();
        pushStyle();
        noFill();
        strokeWeight(2);
        stroke(255, 255, 255);
        beginShape();
        curveVertex(0, 0);
        bspnodes[noderes * iy + ix].visualA();
        curveVertex(0, 0);
        endShape();
        popStyle();

        popMatrix();
      }
    }
    redraw=false;
  }
  tock();  //no touch
}

/**
 ================================
 Mandatory Code
 ================================
 **/
void keyPressed() {
  if (key == 'r' || key == 'R') {
    recording = !recording;
  }
  if (key == 'a' || key == 'A') {
    saveFrame("output/frame_####.png");
    for (BSPNode b : bspnodes) {

      b.reset();
      redraw=true;
    }
  }
}
void tick() {
  currentTime = millis()*timeScale;
}
void tock() {
  if (recording) {
    saveFrame("output/frame_####.png");
  }
  //ui();
}
void ui() {
  drawFPS(255);
}
void drawFPS(int textColor)
{
  pushMatrix();
  noStroke();
  fill(0);
  rect(0, 0, 100, 40);
  fill(textColor);
  text(frameRate, 0, 15);
  popMatrix();
}

// linear interpolate two colors in HSB space
color lerpColorHSB(color c1, color c2, float amt) {
  amt = min(max(0.0, amt), 1.0);
  float h1 = hue(c1), s1 = saturation(c1), b1 = brightness(c1);
  float h2 = hue(c2), s2 = saturation(c2), b2 = brightness(c2);
  // figure out shortest direction around hue
  float z = g.colorModeZ;
  float dh12 = (h1>=h2) ? h1-h2 : z-h2+h1;
  float dh21 = (h2>=h1) ? h2-h1 : z-h1+h2;
  float h = (dh21 < dh12) ? h1 + dh21 * amt : h1 - dh12 * amt;
  if (h < 0.0) h += z;
  else if (h > z) h -= z;
  colorMode(HSB);
  return color(h, lerp(s1, s2, amt), lerp(b1, b2, amt));
}
