xxxxxxxxxx
// recursion, translate, map, rotate, random, random Gaussian, binary tree
// Mouse click for a new tree - takes a few seconds.
int trunkLength = int(random(50, 150));
boolean firstTime = true;
void setup() {
size(550, 550);
noLoop();
}
void draw() {
drawPaper(1500);
translate(width/2, height-50);
branch(trunkLength);
}
void branch(float len) {
float theta = random(0, PI/3);
float swt = map(len, 1.5, 120, .1, 3);
strokeWeight(swt);
drawLine(0, 0, 0, -len, swt);
translate(0, -len);
len *= 0.66;
if (firstTime) {
len = 100;
firstTime = false;
}
if (len > 1.5) {
pushMatrix();
rotate(theta);
branch(len);
popMatrix();
pushMatrix();
rotate(-theta);
branch(len);
popMatrix();
}
}
void drawLine(float strtx, float strty, float finx, float finy, float swIn) {
int numSegs = 50;
float fraction = 0;
int divBy = numSegs;
float x1 = strtx;
float y1 = strty;
float sw ;
if (firstTime) sw = 3.5;
else sw = swIn;
stroke(50);
//noFill();
strokeWeight(sw);
beginShape();
for (int j = 0; j <= int (numSegs); j++) {
float x = (randomGaussian()*.3) + x1 + (fraction * (finx-x1));
float y = (randomGaussian()*.3) + y1 + (fraction * (finy-y1));
vertex(x, y);
x1 = x;
y1 = y;
fraction = 1.0/divBy--;
}
endShape();
}
void drawPaper(int numLines) {
background(#CCCCCC);
for (int i = 0; i < numLines; i++) {
strokeWeight(2);
stroke(random(240, 260), random( 5, 10));
float strty, finy, xdist = random(5, 11), finx = 0;
strty = finy = random(height);
for (float strtx = 0; strtx <= width+20; strtx += xdist) {
line( strtx, strty, finx, finy);
finx = strtx;
finy = strty;
strty += random(-10, 10);
}
}
}
void mousePressed() {
firstTime = true;
trunkLength = int(random(50, 150));
redraw();
}