// Java in the Processing environement: www.processing.org tree t1,t2,t3; void setup(){ size(1200,1000); t1 = new tree(); colorMode(HSB,360,100,100); textAlign(CENTER); textSize(42); } void draw(){ background(210,30,100); t1.display(width*0.5,height); fill(0); text("Press any key for a new tree.",width*0.5,50); } void keyPressed(){ t1 = new tree(); } // The tree class class tree{ // lots of randomized parameters: float trunkLen; float branchAngle; int maxDepth; float shrink; // Each tree has it's own random seed to redraw identically long seed = minute()+second(); float baseHue; float fruitProb; float fruitSize; float leafLength; float leafWidth; float leafHue; tree(){ // for a new tree pick new parameters from preset ranges: trunkLen = random(150,200); branchAngle = random(0.2,0.5); maxDepth = int(random(7,10)); shrink = random(0.7,0.8); baseHue = random(0,90); fruitProb = random(10,90); fruitSize = random(20,40); leafLength = random(20,80); leafWidth = random(0.2,0.9); leafHue = random(0,360); } // draw leaf void leaf(){ strokeWeight(1); stroke(0); fill(leafHue,255,255); ellipse(leafLength*0.5,0,leafLength,leafLength*leafWidth); line(0,0,leafLength*0.5,0); } // draw fruit void fruit(){ strokeWeight(1); stroke(0); fill((leafHue+180)%360,255,255); ellipse(fruitSize*0.5,0,fruitSize,fruitSize); } void branches(float len, float depth, float z){ // stop if branches are too short or max depth is reached. if(len < 2 || depth > maxDepth){ if(random(0,100) < fruitProb){ fruit(); } else { // add a loop and some rotates to draw multiple leaves/branch leaf(); } return; } strokeWeight(len*0.2); stroke(baseHue,255,z); line(0,0,len,0); translate(len,0); pushMatrix(); rotate(-1*branchAngle*random(0.5,1.5)); branches(len*shrink*random(0.8,1.2), depth+1,z-50/maxDepth); popMatrix(); pushMatrix(); rotate(branchAngle*random(0.5,1.5)); branches(len*shrink*random(0.8,1.2), depth+1,z-50/maxDepth); popMatrix(); } void display(float x, float y){ randomSeed(seed); noStroke(); pushMatrix(); translate(x,y); rotate(-PI*0.5); branches(trunkLen,0,50); popMatrix(); } }