grove
@author
void setup(){
size(640, 480, P3D);
mousePressed();
}
void mousePressed(){
background(60, 179, 113);
translate(width / 2, height * (4.0 / 5), -300);
stroke(255);
for(int i = 0; i < 6; i++){
pushMatrix();
translate(random(-width / 2, width / 2), 0, random(width / 2));
new Tree().display();
popMatrix();
}
}
void draw(){
}
class Tree{
float stem, openness, scale;
int branchNum, depth, maxDepth;
ArrayList<Tree> branches;
Tree(){
stem = random(30, 70);
openness = random(0, 30);
branchNum = int(random(2, 8));
depth = 0;
maxDepth = int(random(2, 6));
branches = new ArrayList<Tree>();
for(int i = 0; i < branchNum; i++){
branches.add(new Tree(depth + 1, maxDepth));
}
}
Tree(int _depth, int _maxDepth){
stem = random(30, 100);
openness = random(0, 60);
branchNum = int(random(1, 4));
depth = _depth;
maxDepth = _maxDepth;
branches = new ArrayList<Tree>();
if(depth < maxDepth){
for(int i = 0; i < branchNum; i++){
branches.add(new Tree(depth + 1, maxDepth));
}
}
}
void display(){
pushMatrix();
for(int bi = 0; bi < branchNum; bi++){
pushMatrix();
float radian1 = radians(openness);
float radian2 = TWO_PI * (float(bi) / branchNum);
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(!branches.isEmpty()){
translate(x, y, z);
rotateX(atan2(z, y));
rotateZ(atan2(x, y));
branches.get(bi).display();
}
popMatrix();
}
popMatrix();
}
}