Fraktals.NL
Code PAGE Chapter 4
Dichotomous Tree
back
// Dichotomous tree
// Code after Casey Reas and Ben Fry
// www.processing.org
let standoff = 0.66,
factor = 0.66;
function setup() {
createCanvas(800, 800);
}
function draw() {
noLoop();
background(255);
stroke(1, 50);
let lengt = 250;
translate(width / 2, height - 30);
// Draw a line 120 pixels
line(0, 0, 0, -lengt);
// Move to the end of that line
translate(0, -lengt);
// Start the recursive branching!
children(lengt);
}
function children(lengt) {
// Each branch will be 2/3rds the size of the previous one
lengt *= factor;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (lengt > 2) {
push(); // Save the current state of transformation (i.e. where are we now)
line(standoff * lengt, 0, -standoff * lengt, 0);
line(standoff * lengt, 0, standoff * lengt, -lengt);
translate(standoff * lengt, -lengt); // Move to the end of the branch
children(lengt); // Ok, now call myself to draw two new branches!!
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
push();
line(-standoff * lengt, 0, -standoff * lengt, -lengt);
translate(-standoff * lengt, -lengt);
children(lengt);
pop();
}
}
back
H Fractal
back
//FraktalH After Casey Reas and Ben Fry
// www.processing.org
var h;
function setup() {
createCanvas(800, 800);
noLoop();
smooth();
}
function draw() {
background(255);
stroke(230);
fill(230);
rect(0, 120, 390, 560);
rect(380, 380, 40, 40);
rect(410, 120, 390, 560);
stroke(0);
// Go to the center of the screen
translate(width / 2, height / 2);
// Start the recursive branching!
branch(300);
}
function branch(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.69;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 1) {
push(); // Save the current state of transformation (i.e. where are we now)
rotate(HALF_PI); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
push();
rotate(-HALF_PI);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
pop();
}
}
Pythagoras Tree
back
function setup(){
createCanvas(800, 500); }
function draw() {
background(255);
stroke(0);
noFill();
noLoop();
side=100;
translate((width-side)/2,height-40);
scale(1.0,-1.0);
rect(0,0,side,side);
translate(0,side);
pyth(side);
}
function pyth(side){
if(side>2){
//draw two rectangles pythagoraswisely
push();
//first rectangle
let newside=sin(QUARTER_PI)*side;
rotate(QUARTER_PI);
rect(0,0,newside,newside);
translate(0,newside);
pyth(newside);
//recursive call of the function
pop();
//second rectangle
translate(side,0);
scale(-1,1);
rotate(QUARTER_PI);
rect(0,0,newside,newside);
translate(0,newside);
pyth(newside);
//recursive call of the function side=newside;
}
}
back