final int NB_TILES_SIDE = 3;
final int NB_POINTS_SIDE = NB_TILES_SIDE+1;
final float TILE_SIZE = 150;
final int HEIGHT_H = 150;
PVector[][] randomPoints;
void initPointsAndTrees()
points = new PVector[NB_POINTS_SIDE][NB_POINTS_SIDE];
randomPoints = new PVector[NB_POINTS_SIDE][NB_POINTS_SIDE];
for (int i = 0; i < NB_POINTS_SIDE; i++)
for (int j = 0; j < NB_POINTS_SIDE; j++)
randomPoints[i][j] = new PVector(random(.1, .9) * TILE_SIZE, random(.1, .9) * TILE_SIZE);
for (int i = 0; i < NB_POINTS_SIDE; i++)
for (int j = 0; j < NB_POINTS_SIDE; j++)
points[i][j] = new PVector(i*TILE_SIZE - TILE_SIZE*NB_TILES_SIDE/2 + randomPoints[i][j].x,
j*TILE_SIZE - TILE_SIZE*NB_TILES_SIDE/2 + randomPoints[i][j].y,
trees = new Tree[NB_TILES_SIDE][NB_TILES_SIDE];
for (int i = 0; i < NB_TILES_SIDE; i++)
for (int j = 0; j < NB_TILES_SIDE; j++)
trees[i][j] = new Tree(points[i][j], points[i+1][j], points[i+1][j+1], points[i][j+1]);
for (int i = 0; i < NB_TILES_SIDE; i++)
for (int j = 0; j < NB_TILES_SIDE; j++)
deltaH += trees[i][j].deltaH;
deltaH /= (NB_TILES_SIDE*NB_TILES_SIDE);
translate(width/2, height/2);
rotateX(TWO_PI * mouseY / height);
rotateZ(TWO_PI * mouseX / width);
for (int i = 0; i < NB_TILES_SIDE; i++)
for (int j = 0; j < NB_TILES_SIDE; j++)
drawTree(trees[i][j].root);
void drawTree(Node p_node)
if (p_node.depth == MAX_DEPTH)
void drawTriangles(Node p_node)
fill(p_node.colorNode, 200);
stroke(p_node.colorNode/2, 50);
vertex(A.x, A.y, A.z-deltaH);
vertex(H.x, H.y, H.z-deltaH);
vertex(B.x, B.y, B.z-deltaH);
vertex(B.x, B.y, B.z-deltaH);
vertex(H.x, H.y, H.z-deltaH);
vertex(C.x, C.y, C.z-deltaH);
vertex(C.x, C.y, C.z-deltaH);
vertex(H.x, H.y, H.z-deltaH);
vertex(D.x, D.y, D.z-deltaH);
vertex(D.x, D.y, D.z-deltaH);
vertex(H.x, H.y, H.z-deltaH);
vertex(A.x, A.y, A.z-deltaH);
Node nodeA, nodeB, nodeC, nodeD;
Node(PVector p_A, PVector p_B, PVector p_C, PVector p_D, int p_depth)
Tree(PVector p_A, PVector p_B, PVector p_C, PVector p_D)
root = new Node(p_A, p_B, p_C, p_D, 0);
void addNodes(Node p_node)
int depth = p_node.depth + 1;
PVector H = new PVector((A.x + B.x + C.x + D.x)/4, (A.y + B.y + C.y + D.y)/4, (A.z + B.z + C.z + D.z)/4);
H.z += random(-HEIGHT_H/(depth*depth), HEIGHT_H/(depth*depth));
deltaH = (lowestH + highestH)/2;
p_node.colorNode = (int)map(min(abs(H.z), HEIGHT_H), 0, HEIGHT_H, 0, 255);
float u = random(.1, .9);
float v = random(.1, .9);
PVector AB = new PVector(A.x + u*(B.x-A.x), A.y + u*(B.y-A.y), A.z + u*(B.z-A.z));
PVector BC = new PVector(B.x + u*(C.x-B.x), B.y + u*(C.y-B.y), B.z + u*(C.z-B.z));
PVector CD = new PVector(C.x + u*(D.x-C.x), C.y + u*(D.y-C.y), C.z + u*(D.z-C.z));
PVector DA = new PVector(D.x + u*(A.x-D.x), D.y + u*(A.y-D.y), D.z + u*(A.z-D.z));
Node nodeA = new Node(A, AB, H, DA, depth);
Node nodeB = new Node(B, BC, H, AB, depth);
Node nodeC = new Node(C, CD, H, BC, depth);
Node nodeD = new Node(D, DA, H, CD, depth);