• fullscreen
  • Particle.pde
  • UParticleTree01.pde
  • colors.pde
  • gui.pde
  • class Particle {
      UVec3 pos,dir;
      UVertexList path;
      
      float speed,rad;
      float rotD;
      int level,col;
      int stateCnt,stateGoal;
      
      Particle(Particle parent) {
        float x,y,angle;
    
        if(parent==null) { // first level
          rad=10;
          x=width/2;
          y=height;
          level=0;
          speed=startSpeed;
          angle=-HALF_PI;
        }
        else { // inherit qualities from parent
          rad=parent.rad*0.8;
          x=parent.pos.x;
          y=parent.pos.y;
          level=parent.level+1;
          speed=parent.speed*speedMod;
          angle=parent.dir.angle2D();
          angle+=radians(
            random(-branchAngle,branchAngle));
        }
        
        pos=new UVec3(x,y);
        dir=new UVec3(speed,0,0).rotate(angle);
        path=new UVertexList();
        path.add(pos);
        
        stateGoal=(int)random(20,50);
        stateCnt=0;
        
        col=colors.getRandomColor();
    
        rotD=radians(random(0.2,0.5));
        // apply rotD modifier according to current level
        rotD*=map(level,0,maxLevel-1,0,rotMod);
        if(random(100)>50) rotD=-rotD;
      }
      
      void draw() {
        fill(col);
        
        // draw all stored points in path
        for(int i=0; i<path.n; i++) {
          if(i%3==0) {
            noStroke();
            fill(col);
            ellipse(path.v[i].x, path.v[i].y, rad, rad);
          }
          if(i>0) {
            strokeWeight(2);
            stroke(col);
            line(path.v[i].x, path.v[i].y,
              path.v[i-1].x, path.v[i-1].y);
          }
        }
        
        if(stateCnt>stateGoal) return;
        
        // update position and rotation
        pos.add(dir.rotate(rotD));
        
        // store position every other frame
        if(stateCnt%2==0) path.add(pos);
        
        // check state counter
        stateCnt++;
        if(stateCnt==stateGoal && level<maxLevel) {
          int n=(int)random(2,maxBranches+1);
          if(maxBranches==1) n=1;
          for(int i=0; i<n; i++) p.add(new Particle(this));
        }
      }
    }
    
    
    /**
     * UParticleTree01.pde - Marius Watz, 2012
     * Part of Modelbuilder library
     * http://workshop.evolutionzone.com
     *  
     * "Tree" structure drawn by branching particle system.
     *
     * This sketch is an exercise in the chaotic convergence of
     *  multiple parameters. Only a limited subset of possible
     * parameters produce meaningful structures, i.e. "trees".
     */
    import controlP5.*;
    
    import unlekker.util.*;
    import unlekker.modelbuilder.*;
    import ec.util.*;
    import java.util.*;
    
    public ArrayList<Particle> p;
    public int maxLevel=3,maxBranches=4;
    public float startSpeed=3,rotMod=1.5,speedMod=1;
    public float branchAngle=30;
    public boolean doSave;
    
    void setup() {
      size(800,600);  
      smooth();
      
      initGUI();
      reinit();
    }
    
    void draw() {
      background(0);
    
      for(int i=0; i<p.size(); i++) p.get(i).draw();
      
      if(doSave) {
        saveFrame(
          UIO.getIncrementalFilename(
            this.getClass().getSimpleName()+" ###.png",
            sketchPath));
        doSave=false;
      }
      else gui.draw();
    }
    
    public void reinit() {
      initColors();
    
      // create empty arraylist and add one particle
      p=new ArrayList<Particle>();
      p.add(new Particle(null));
    }
    
    UColorTool colors;
    
    public void initColors() {
      colors=new UColorTool();
    
      if (random(100)<50) {
        colors.addGradient(5, 10, "00CCFF", "00FFFF"); // 1
        colors.addGradient(5, 10, "FFFFFF", "FF3300"); // 2
        colors.addGradient(5, 10, "FFFF00", "FF6600"); // 3
    //    colors.addGradient(5, 10, "76BB32", "BAFF00"); // 4
    //    colors.addGradient(5, 10, "004770", "00FFFF"); // 5
    //    colors.addGradient(3, 4, "FF0094", "BE006E"); // 6
      }
      else {
        colors.addGradient(5, 10, "F4F1F1", "DAF0F0"); // 0
        colors.addGradient(5, 10, "0099CC", "00CCFF"); // 1
    //    colors.addGradient(5, 10, "003300", "008668"); // 2
    //    colors.addGradient(5, 10, "FFFF00", "FFDD00"); // 3
        colors.addGradient(5, 10, "FF0099", "FF33CC"); // 4
      }
      
      // generate minimum 8 colors, with 80% chance
      // of excluding any gradient every time
      colors.generateColors(5, 40);
    }
    
    
    USimpleGUI gui;
    
    void initGUI() {
      gui=new USimpleGUI(this);
      gui.addSlider("maxLevel",maxLevel, 3,10);
      gui.addSlider("maxBranches",maxBranches,3,10);
      gui.newRow();
      gui.addSlider("startSpeed",startSpeed,2,5);
      gui.addSlider("branchAngle",branchAngle,15,90);
      gui.newRow();
      gui.addSlider("rotMod",rotMod,0.2,5);
      gui.addSlider("speedMod",speedMod,0.9,1.2);
      gui.newRow();
      gui.addButton("reinit");
      gui.addButton("randomize");
      gui.setHorizontalLayout();
    }
    
    public void randomize() {
      gui.randomizeValue("maxLevel");
      gui.randomizeValue("startSpeed");
      gui.randomizeValue("branchAngle");
      gui.randomizeValue("maxBranches");
      gui.randomizeValue("rotMod");
      gui.randomizeValue("speedMod");
      reinit();
    }  
    
    void keyPressed() {
      if(!online) doSave=true;
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Marius Watz plus+

    UParticleTree01

    Add to Faves Me Likey@! 10
    You must login/register to add this sketch to your favorites.

    "Tree" structure drawn by branching particle system. Part of the Modelbuilder library.

    This sketch is intended as an exercise in the unpredictable convergence of multiple parameters. Only a limited subset of possible
    parameters produce the intended tree structure.

    You need to login/register to comment.