• 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);

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));

}
else { // create 4 pairs of triangles
UVertexList vl=new UVertexList();

(float)height*random(0.3f, 0.7f));
v=vl.v;

}
}

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]);
}

```

### tweaks (0)

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