# Trying to do fractal tree

**URL:** https://discourse.processing.org/t/trying-to-do-fractal-tree/1207
**Category:** Beginners
**Created:** [June 24, 2018, 4:18pm UTC](https://discourse.processing.org/t/trying-to-do-fractal-tree/1207 "2018-06-24T16:18:05Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Nanonnien](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@Nanonnien](https://discourse.processing.org/u/Nanonnien)
#### Post date: [June 24, 2018, 4:18pm UTC](https://discourse.processing.org/t/trying-to-do-fractal-tree/1207/1 "2018-06-24T16:18:05Z")

</div>

Good morning,  
I started watching videos from The coding train channel and I’m trying to replicate his fractal tree. But in a slightly different way:

Sketch.js

```auto
var tree;
var width_canvas = 640;
var height_canvas = 400;

function setup() {

  angle = PI/4;
  coefficient = 0.67;
  createCanvas(width_canvas,height_canvas);
  tree = new Tree()
}

function draw() {
  background(51);
  tree.show();
  }

  function mousePressed(){
    tree.add_branch(angle,coefficient);
  }

```

tree.js

```auto
function Tree() {
  len_branch = 100;
  nb_branch= 0;
  this.tab = [];
  this.tab[0] = new Branch(createVector(width/2,height),createVector(width/2,height-len_branch));

  this.add_branch = function(angle,coefficient) {
    tab_end = this.tab.length;
    for (var i = tab_end-pow(2,nb_branch); i <= tab_end-1; i++) {
      this.tab[2*i+1] =this.tab[i].branch(angle,coefficient);
      this.tab[2*i+2] =this.tab[i].branch(angle,coefficient);
    }
    nb_branch ++;
  }

  this.show=function() {
    for (var i = 0; i <this.tab.length; i++) {
      this.tab[i].show();
    }
  }
}

```

branch.js

```auto
function Branch(begin, end) {

  this.branch=function(angle,coefficient){
  dir = p5.Vector.sub(end,begin);
  dir.rotate(angle);
  dir.mult(coefficient);
  new_end = p5.Vector.add(dir,end);
  branch = new Branch(end, new_end,angle,coefficient);
  return branch;
}

 this.show=function () {
  stroke(255);
  line(begin.x, begin.y, end.x, end.y );
}

}

```

My problem is the following, I only manage to display a part of the lines making up my tree.

I think I miss something with the pop() and push() functions but I can’t understand how they work.
