xxxxxxxxxx
71
let tree;
let tree2;
function setup() {
createCanvas(400, 400);
tree = new Tree(width/2, height/2);
tree2 = new Tree(300, 250);
}
function draw() {
background(220);
tree.draw();
tree2.draw();
noLoop();
}
class Tree{
constructor(x, y){
this.x = x;
this.y = y;
this.div = 4;
}
draw(){
push();
translate(this.x, this.y);
let bottomLeft = createVector(-30, -10);
let bottomRight = createVector(30, -10);
let topCenter = createVector(0, -100);
// bottom
line(bottomLeft.x, bottomLeft.y, bottomRight.x, bottomRight.y);
// right
line(bottomRight.x, bottomRight.y, topCenter.x, topCenter.y);
// left
line(topCenter.x, topCenter.y, bottomLeft.x, bottomLeft.y);
let rightSegmentPiece = p5.Vector.sub(bottomRight, topCenter).div(this.div);
let leftSegmentPiece = p5.Vector.sub(topCenter, bottomLeft).div(this.div);
for(let x = 1 ; x < this.div ; x++){
// TOP DOWN
// straight down the middle
line(topCenter.x,
topCenter.y,
topCenter.x,
bottomLeft.y);
// right half
line(topCenter.x + (rightSegmentPiece.x * x),
topCenter.y + (rightSegmentPiece.y * x),
topCenter.x + (rightSegmentPiece.x * x),
bottomLeft.y);
// left half
line(bottomLeft.x + (leftSegmentPiece.x * x),
bottomLeft.y + (leftSegmentPiece.y * x),
bottomLeft.x + (leftSegmentPiece.x * x),
bottomLeft.y);
// SIDEWAYS
// left half
line(topCenter.x - (leftSegmentPiece.x * x),
topCenter.y - (leftSegmentPiece.y * x),
topCenter.x + (rightSegmentPiece.x * x),
topCenter.y + (rightSegmentPiece.y * x));
}
pop();
}
}