/** a blooming tree by Guru */ import java.util.List; import java.util.ArrayList; List branches; void setup() { branches = new ArrayList(); Branch b = new Branch(); branches.add(b); size(300,300); smooth(); background(255); fill(0); } void draw() { List dead = new ArrayList(); for( int i = 0; i < branches.size(); i++ ) { Branch b = ((Branch)branches.get(i)); if (!b.isDead()) { b.draw(); if ( random(1, 100) > 97 ) { branches.add(b.branch()); } } else { b.bloom(); dead.add(b); } } for( int i = 0; i < dead.size(); i++ ) { branches.remove( dead.get(i)); } } class Branch { float s = 20; float x = 150; float y = 300; float step = 5; float a = 0; float astep = 0.075; public boolean isDead() { return s < 0.1; } public Branch() { } public Branch( float x, float y, float s, float a, float step, float astep ) { this.x = x; this.y = y; this.s = s; this.a = a; this.step = step; this.astep = astep; } public Branch branch() { return new Branch( x, y, s * 0.8, a, step *0.8, astep * -1 ); } void draw() { ellipse( x, y, s, s ); step *= 0.98; if ( random(1, 100) > ( cos(a) > 0 ? 80 : 90 ) ) { astep *= -1; } a += astep; y -= cos( a ) * step; x += sin( a ) * step; s *= 0.98; } void bloom() { fill(255, 40); stroke(255,0,0, 10); ellipse(x,y,9,9); fill(0); stroke(0); } }