3D recursive tree
@author
Tree tree;
void setup(){
size(640, 480, P3D);
tree = new Tree();
}
void draw(){
background(60);
translate(width / 2, height * (4.0 / 5));
rotateX(map(mouseY, 0, width, -PI / 4, PI / 4));
rotateY(frameCount * 0.003);
stroke(255);
tree.display();
}
class Tree{
float stem, openness, scale;
int branch, depth, maxDepth;
Tree(){
stem = 100.0;
openness = 30;
scale = 0.7;
branch = 5;
depth = 0;
maxDepth = 4;
}
Tree(float _stem, float _openness, float _scale,
int _branch, int _depth, int _maxDepth){
stem = _stem;
openness = _openness;
scale = _scale;
branch = _branch;
depth = _depth;
maxDepth = _maxDepth;
}
void display(){
pushMatrix();
for(int bi = 0; bi < branch; bi++){
pushMatrix();
float radian1 = radians(openness);
float radian2 = TWO_PI * (float(bi) / branch);
float x = stem * sin(radian1) * cos(radian2);
float y = -stem * cos(radian1);
float z = stem * sin(radian1) * sin(radian2);
strokeWeight(maxDepth - depth + 1);
line(0, 0, 0, x, y, z);
if(depth < maxDepth){
translate(x, y, z);
rotateX(atan2(z, y));
rotateZ(atan2(x, y));
new Tree(stem * scale, openness, scale, branch, depth + 1, maxDepth).display();
}
popMatrix();
}
popMatrix();
}
}