Pages

duminică, 14 martie 2021

OpenProcessing - simple tree_001.

This is modified example from this in order to build another good tree using openprocessing website interface.
You can see this example on my account.
Let's see the source code:
/*--based on https://openprocessing.org/sketch/396851--*/

var nbranchs = 73;
function setup() {
  createCanvas(640, 400);
  background(0, 0, 128);
  
  noFill();
  stroke(128);
  noLoop();
}

function draw() {
  tree(100, 200, 150, 350);
}
function mousePressed() {
  background(0, 0, 128);
  redraw();
}

function branch(x, y, dx, dy) {
  var sign = random(dx+1.0)/(abs(dx)+5.0);
  
  for (var i = 0; i <= x; i += 3) {
    var idx = i/x;
    
    var xi = bezierPoint(x, x + dx/2,   x + dx, x + dx, idx);
    var yi = bezierPoint(y, y, y + dy, y + dy, idx);

    line(xi, yi, xi + sign*random(8), yi + random(18));  
	
  }
}

function tree(left, right, top, bottom) {
  for (var idx = 0; idx < nbranchs; idx += 1) {
    
    // choose a random y position
    var y = random(bottom, top);
		
    
    // choose a random x position inside of a triangle
    var dx = map(y, bottom, top, 0.0, (right-left)/2.0);
    var x = random(left + dx, right - dx);
		
    var x1 = random(left + dx, right - dx);
    
		// choose the size of the branch according to the position on the tree
    var w = map (x, left, right, random(-25) -25, random(25)+25) + 1;
    var h = map (y, bottom, top, 5 +random(20), 5);
    
		var w1 = map (x, left, right, random(-35), random(35)) + 3;
    var h1 = map (y, bottom, top, random(10), random(10));
    // randonize the size
    var dw = random(-10, 5);
    var dh = random (-5, 5);
  	
		var dw1 = random(-5, 1);
    var dh1 = random (-1, 1);
    // create the new branch
		branch(x1, y, w1 + dw1, h1 + dh1);
		stroke(random(0),random(128),random(15));
		branch(x1, y, w1 + dw1, h1 + dh1);
		stroke(random(25),random(255),random(50));
    branch(x, y, w + dw, h + dh);
    
  }
}