• fullscreen
  • Poly.pde
  • UCracking01.pde
  • crack.pde
  • class Poly {
      UVec3 v[];
      int col;
    
      Poly(UVec3 v1, UVec3 v2, UVec3 v3) {
        v=new UVec3[3];
        v[0]=v1;
        v[1]=v2;
        v[2]=v3;
    
        col=color(random(50, 100)+(random(100)>50?185:0));
        if(random(100)>80)
          col=UColorTool.interpolate(random(1),"00FFFF","003366");
        else if(random(100)>80)
          col=UColorTool.interpolate(random(1),"FF6600","FFCC00");
      }
    
      public void draw() {
        fill(col);
        beginShape(TRIANGLES);
        vertex(v[0].x, v[0].y);
        vertex(v[1].x, v[1].y);
        vertex(v[2].x, v[2].y);
        endShape();
      }
    
      // calculate random point in triangle, tending towards the
      // center of the triangle surface
      public UVec3 calcRandomPoint() {
        UVec3 vv=UVec3.interpolate(v[1], v[2], UUtil.rnd.random(0.33f, 0.66f));
        vv=UVec3.interpolate(v[0], vv, UUtil.rnd.random(0.33f, 0.66f));
        return vv;
      }
    
      // subdivide Poly by calculating a random point in the triangle
      // face and using it to produce three new Poly instances
      public Poly[] subdivide() {
        UVec3 vv=calcRandomPoint();
        Poly p[]=new Poly[3];
        p[0]=new Poly(v[0], v[1], vv);
        p[1]=new Poly(v[1], v[2], vv);
        p[2]=new Poly(v[2], v[0], vv);
        return p;
      }
    }
    
    
    /**
     * UCracking01.pde - Marius Watz, 2012
     * http://workshop.evolutionzone.com
     * 
     * Cracking (aka subdivision) of triangular geometry.
     *
     */
     
    
    import controlP5.*;
    
    import java.util.ArrayList;
    
    import unlekker.modelbuilder.*;
    import unlekker.util.*;
    
    public ArrayList<Poly> f;
    public boolean doCrack,doSave;
    public USimpleGUI gui;
    
    public void setup() {
      size(800, 600);
      
      gui=new USimpleGUI(this);
      gui.addButton("reinit");
      gui.addToggle("doCrack",doCrack);
      
      reinit();
      smooth();
    }
    
    public void draw() {
      background(0);
      stroke(0,128);
      
      for (Poly pp:f) pp.draw();
    
      if (doCrack) crack();
      
      if(doSave) {
        saveFrame(
        UIO.getIncrementalFilename(
          this.getClass().getSimpleName()+" ###.png",
          sketchPath));
        doSave=false;
      }
      else     
        gui.draw();
    }
    
    public void keyPressed() {
      if(!online && key=='s') doSave=true;
    }
    
    public void mousePressed() {
      if(!gui.isMouseOver()) crack();
    }
    
    
    // initializes triangle surface in one of two configurations
    public void reinit() {
      doCrack=false;
      
      UVec3 v[];
      f=new ArrayList<Poly>();
      
      if(random(100)>50) { // edges and center point
        v=UVec3.getVec3(5);
        v[0]=new UVec3(0, 0);
        v[1]=new UVec3(width-1, 0);
        v[2]=new UVec3(width-1, height-1);
        v[3]=new UVec3(0, height-1);
        v[4]=new UVec3((float)width*random(0.3f, 0.7f), 
          (float)height*random(0.3f, 0.7f));
      
        f.add(new Poly(v[0], v[1], v[4]));
        f.add(new Poly(v[1], v[2], v[4]));
        f.add(new Poly(v[2], v[3], v[4]));
        f.add(new Poly(v[3], v[0], v[4]));
      }
      else { // create 4 pairs of triangles
        UVertexList vl=new UVertexList();
        vl.add(0, 0);
        vl.add((float)width*random(0.33,0.66), 0);
        vl.add(width-1, 0);
        vl.add(width-1,(float)height*random(0.2,0.8));
        
        vl.add(width-1, height-1);
        vl.add((float)width*random(0.2,0.8), height-1);
        vl.add(0, height-1);
        vl.add(0,(float)height*random(0.2,0.8));
        
        vl.add((float)width*random(0.3f, 0.7f), 
          (float)height*random(0.3f, 0.7f));
        v=vl.v;
    
        f.add(new Poly(v[7], v[0], v[1]));
        f.add(new Poly(v[1], v[8], v[7]));
        f.add(new Poly(v[1], v[8], v[3]));
        f.add(new Poly(v[1], v[2], v[3]));
        f.add(new Poly(v[3], v[4], v[5]));
        f.add(new Poly(v[3], v[5], v[8]));
        f.add(new Poly(v[5], v[6], v[7]));
        f.add(new Poly(v[5], v[7], v[8]));
      }
    }
    
    void crack() {
      ArrayList<Poly> ff=new ArrayList<Poly>();
    
      // choose one of the first four triangles in arraylist
      Poly pp=f.get((int)random(4));
      
      // subdivide triangle to produce new triangles
      Poly p[]=pp.subdivide();
      
      // remove old triangle from arraylist
      f.remove(pp);
      
      // add the new triangles to arraylist
      for (int i=0; i<p.length; i++) f.add(p[i]);
    }
    
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    Marius Watz plus+

    UCracking01

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

    Cracking (aka subdivision) of triangular geometry. Uses ArrayList to efficiently handle storage of new triangles while removing old ones after they've been subdivided.

    Some other examples of 2D subdivision:
    http://flickr.com/photos/soulwire/sets/72157624556701912/
    http://flickr.com/photos/lennyjpg/3170344895/

    You need to login/register to comment.