Bifurcator bif; LGraph lg; ParamSet p; Stack pStack; Animator anim; int xStart; int yStart; boolean zooming = false; boolean animating = false; color lineRed = color(255, 74, 0); // Font stuff PFont font; // Button stuff boolean locked = false; Button zoomInButton; Button zoomOutButton; Button animateButton; Button stopButton; Button clearButton; void setup() { font = loadFont("AndaleMono-10.vlw"); size(800, 650); bif = new Bifurcator(0, 0, width, 300); p = new ParamSet(1, 4, 0, 1); bif.setP(p); bif.update(); bif.render(); lg = new LGraph(0, 300, width-1, 300); lg.setP(p); lg.update(); // Draw buttons color buttonColor = color(220); color buttonColorHighlight = color(255); zoomInButton = new RectButton(5, 600+5, 26, buttonColor, buttonColorHighlight); zoomOutButton = new RectButton(36, 600+5, 26, buttonColor, buttonColorHighlight); animateButton = new RectButton(68, 600+5, 26, buttonColor, buttonColorHighlight); stopButton = new RectButton(68+26+6, 600+5, 26, buttonColor, buttonColorHighlight); clearButton = new RectButton(68+26+6+26+6, 600+5, 26, buttonColor, buttonColorHighlight); zoomInButton.im = loadImage("zoom-in.png"); zoomOutButton.im = loadImage("zoom-out.png"); animateButton.im = loadImage("play.png"); stopButton.im = loadImage("stop.png"); clearButton.im = loadImage("clear.png"); } void mousePressed() { xStart = mouseX; yStart = mouseY; if(zoomInButton.pressed()) { if(zooming == true) { zooming = false; zoomInButton.pressed = false; } else { zooming = true; zoomInButton.pressed = true; } } else if(zoomOutButton.pressed()) { clearSaved(); if(pStack.size() > 0) { cursor(WAIT); p = (ParamSet)pStack.pop(); bif.setP(p); lg.setP(p); bif.update(); lg.update(); } cursor(ARROW); } else if(animateButton.pressed()) { clearSaved(); animate(); } else if(stopButton.pressed()) { reset(); } else if(clearButton.pressed()) { clearSaved(); } } void mouseReleased() { if(zooming && abs(mouseX - xStart) > 5 && abs(mouseY - yStart) > 5) { float rs1 = bif.xPosToR(xStart); float rs2 = bif.xPosToR(mouseX); float ps1 = bif.yPosToP(yStart); float ps2 = bif.yPosToP(mouseY); // Push the current ParamSet onto the stack pStack.push(p); p = new ParamSet(min(rs1, rs2), max(rs1, rs2), min(ps1, ps2), max(ps1, ps2)); bif.setP(p); lg.setP(p); print(bif.toString()); cursor(WAIT); bif.update(); lg.update(); cursor(ARROW); zoomInButton.pressed = false; zooming = false; } } void draw() { update(mouseX, mouseY); if(animating) { stopButton.display(); anim.update(); return; } else { // Paint over the bif // Must go in this order lg.render(); bif.render(); } // Draw buttons fill(180); stroke(0); rect(0, 600, width-1, 50); zoomInButton.display(); zoomOutButton.display(); animateButton.display(); stopButton.display(); clearButton.display(); float rR = round(p.rCurrent*10000) / 10000.0; fill(0); String eqText = "P[n+1] = "+rR+" * P[n] * (1 - P[n])"; textFont(font, 10); text(eqText, 400, bif.h + lg.h + 15); // Draw the selection box if the mouse is pressed if(zooming && mousePressed && overRect(bif.xPos, bif.yPos, bif.w, bif.h)) { noFill(); stroke(color(0)); rect(xStart, yStart, min(mouseX, bif.w)-xStart, min(mouseY, bif.h)-yStart); } if(!zooming && mousePressed && overRect(bif.xPos, bif.yPos, bif.w, bif.h)) { if(mouseButton == RIGHT) p.pInit = bif.yPosToP(mouseY); else p.rCurrent = bif.xPosToR(mouseX); lg.update(); } } float logistic(float r, float pop) { return r * pop * (1 - pop); } boolean overRect(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) return true; else return false; } void animate() { anim = new Animator(p, bif, lg); animating = true; framerate(20); } void reset() { framerate(30); animating = false; } void clearSaved() { lg.bgSaved = null; p.rValues = new Vector(); } void keyPressed() { if(key == CODED) { if(keyCode == LEFT) p.rCurrent -= 0.0001; else if(keyCode == RIGHT) p.rCurrent += 0.0001; else if(keyCode == UP) p.pInit = min(p.pMax, p.pInit + 0.001); else if(keyCode == DOWN) p.pInit = max(p.pMin, p.pInit - 0.001); lg.update(); } else if(key == ' ') { SavedR sr = new SavedR(p.rCurrent, color(random(0, 255), random(0, 255), random(0, 255))); p.rValues.add(sr); lg.saveBG(sr.col); } } void update(int x, int y) { if(locked == false) { zoomInButton.update(); zoomOutButton.update(); animateButton.update(); stopButton.update(); clearButton.update(); } else { locked = false; } if(zooming && overRect(bif.xPos, bif.yPos, bif.w, bif.h)) cursor(CROSS); else cursor(ARROW); }