createCanvas(windowWidth, windowHeight, P2D);
colorMode(HSB, 360, 255, 255);
for (var i=0; i<nh; i++) {
for (var j=0; j<nv; j++) {
for (var i=0; i<nv*nh; i++) {
function mouseReleased() {
function createTree(i, j) {
var x=.1*width+i*int(.9*width/nh);
var y=int(.2*height+j*int(.8*height/nv));
var start=createVector(x, y);
trees[i+j*nh]=new Tree(start, start.y/(height-130), i+j*nh);
trees[i+j*nh].branches[0]=new Branche(start, 15*sqrt(start.y/height), 0, 1, i+j*nh);
function Tree(start, coeff, index) {
this.proba1=random(.8, 1);
this.proba2=random(.8, 1);
this.proba3=random(.4, .5);
this.proba4=random(.4, .5);
Tree.prototype.grow=function() {
for (var i=0; i<this.branches.length; i++) {
function Branche(start, stw, angle, gen, index) {
this.position=start.copy();
this.speed=createVector(0, -3);
this.maxlife=maxlife*random(.3, .8);
this.proba1=trees[this.index].proba1;
this.proba2=trees[this.index].proba2;
this.proba3=trees[this.index].proba3;
this.proba4=trees[this.index].proba4;
this.deviation=random(.2, .7);
Branche.prototype.grow=function() {
if (this.age==int(this.maxlife/this.gen)||random(1)<.05*this.gen) {
var brs=trees[this.index].branches;
var c=trees[this.index].coeff;
if (random(1)<this.proba1/this.gen)brs.push(new Branche(createVector(this.position.x, this.position.y), this.stw*random(.2, 1), this.angle+random(.7, 1.1)*this.deviation, this.gen+.1, this.index));
if (random(1)<this.proba2/this.gen)brs.push(new Branche(createVector(this.position.x, this.position.y), this.stw*random(.2, 1), this.angle-random(.7, 1.1)*this.deviation, this.gen+.1, this.index));
if (random(1)<this.proba3/this.gen)brs.push(new Branche(createVector(this.position.x, this.position.y), this.stw*random(.5, .8), this.angle+random(.2, 1)*this.deviation, this.gen+.1, this.index));
if (random(1)<this.proba4/this.gen)brs.push(new Branche(createVector(this.position.x, this.position.y), this.stw*random(.5, .8), this.angle-random(.2, 1)*this.deviation, this.gen+.1, this.index));
this.speed.x+=random(-.5, .5);
Branche.prototype.display=function() {
var c=trees[this.index].coeff;
var st=trees[this.index].start;
this.position.x+=-this.speed.x*cos(this.angle)+this.speed.y*sin(this.angle);
this.position.y+=this.speed.x*sin(this.angle)+this.speed.y*cos(this.angle);
stroke(trees[this.index].teinte+this.age+10*this.gen, 0, 0, .04);
strokeWeight(map(this.age, 0, this.maxlife, this.stw*1.3, this.stw*.9));
var dis=.005*pow(st.y-y0, 1.8);
line(x0+dis*random(.5, 1.2), 2*st.y-y0+dis*random(.5, 1.2), this.position.x+dis*random(.5, 1.2), 2*st.y-this.position.y+dis*random(.5, 1.2));
line(x0+dis*random(.5, 1.2), 2*st.y-y0+dis*random(.5, 1.2), this.position.x+dis*random(.5, 1.2), 2*st.y-this.position.y+dis*random(.5, 1.2));
strokeWeight(map(this.age, 0, this.maxlife, this.stw, this.stw*.6));
stroke(trees[this.index].teinte+this.age+20*this.gen, 150*c, 200+20*this.gen, 15*c);
line(x0+.1*this.stw, y0, this.position.x+.1*this.stw, this.position.y);
stroke(trees[this.index].teinte+this.age+20*this.gen, 100*c, 50+20*this.gen, 15*c);
strokeWeight(map(this.age, 0, this.maxlife, this.stw, this.stw*.6));
line(x0, y0, this.position.x, this.position.y);