Generating the tree only once

Hello,

I am trying to make a tree for a project. I made something but I can't get to work properly. I want to generate the tree every time once and not every frame rate. Later I want to try to let the tree move so I have to put the function in the draw. How can I make it that the three is only generating once instead of every frame a new tree?

I put the code down below. Thanks a lot.

Tree Tree1;
void setup() {
  size(800, 800);

}

void draw() {
  background(255);

  Tree1.tree(width/2, height, height/5, -HALF_PI);

}



class Tree {    // creates the class
  float beginx, beginy, blength, aangle;
  color treeColor = color(41, 25, 3);

  Tree (float beginX, float beginY, float bLength, float angle) {   //The constructor of the class
    beginx=beginX;
    beginy=beginY;
    blength=bLength;
    aangle=angle;
  }

  void move() {
  }

  void tree(float beginX, float beginY, float bLength, float angle)
  {
    //find the end of the branch
    float endX = beginX + bLength*cos(angle);
    float endY = beginY + bLength*sin(angle);

    //draw the branch
    strokeWeight(map(bLength, height/4, 3, 20, 1));
    stroke(treeColor);
    line(beginX, beginY, endX, endY);

    //generate 2 new branchs
    if (bLength  > 3)
    {
      if (random(1) > 0.1) tree(endX, endY, bLength*random(0.6, 0.8), angle - random(PI/15, PI/5));
      if (random(1) > 0.1) tree(endX, endY, bLength*random(0.6, 0.8), angle + random(PI/15, PI/5));
    }
  }
}

I hope some one can help..

Niek Boersen

Tagged:

Answers

  • edit post, highlight code, pess ctrl-o to format your code...

    move the tree building code to the Tree constructor.

  • Tree Tree1
    

    classes traditionally start with a capital, so Tree is ok.

    instances traditionally start with a lower case letter, so Tree1 would be better as tree1.

  • Okay I editted the post, didn't know how to do that. Thanks.

    Move the tree building code to the Tree constructor? What do you mean by that?

  • Move the code that sets up a Tree into the constructor. This includes the creation of any random values - you may need to store then in additional class variables! Keep the code that draws the tree in the tree() class method, and then rename that method to draw().

  • Mmh I don't really get what you mean by that:

    This is what I have now but it doesn't work.

    void setup() {
      size(800, 800);
      tree1= new Tree(width/2, height, height/5, -HALF_PI);
    }
    
    void draw() {
      background(255);
      tree1(width/2, height, height/5, -HALF_PI);
    }
    
    
    
    Tree tree1;
    class Tree {    // creates the class
      float beginx, beginy, blength, aangle;
      color treeColor = color(41, 25, 3);
    
      Tree (float beginX, float beginY, float bLength, float angle) {   //The constructor of the class
        beginx=beginX;
        beginy=beginY;
        blength=bLength;
        aangle=angle;
    
        float endX = beginX + bLength*cos(angle);
        float endY = beginY + bLength*sin(angle);
    
        //draw the branch
        strokeWeight(map(bLength, height/4, 3, 20, 1));
        stroke(treeColor);
        line(beginX, beginY, endX, endY);
    
        //generate 2 new branchs
        if (bLength  > 3)
        {
          if (random(1) > 0.1) Tree(endX, endY, bLength*random(0.6, 0.8), angle - random(PI/15, PI/5));
          if (random(1) > 0.1) Tree(endX, endY, bLength*random(0.6, 0.8), angle + random(PI/15, PI/5));
        }
      }
    }
    
  • You will need your Tree class to have two additional Tree objects as class variables to hold the Trees that are the left and right branches:

    class Tree { // Defines what a Tree is.
    
      float beginx, beginy, blength, aangle;
      float endX, endY;
      color treeColor = color(41, 25, 3);
      Tree l_branch; // This tree might have other trees inside it!
      Tree r_branch; // These are the BRANCHES!
    
      Tree (float beginX, float beginY, float bLength, float angle) { // The constructor is called when a tree is created.
        beginx=beginX;
        beginy=beginY;
        blength=bLength;
        aangle=angle;
    
        endX = beginX + bLength*cos(angle);
        endY = beginY + bLength*sin(angle);
        //generate 2 new branchs
        if (bLength  > 3)
        {
          if (random(1) > 0.1) l_branch = new Tree(endX, endY, bLength*random(0.6, 0.8), angle - random(PI/15, PI/5));
          if (random(1) > 0.1) r_branch = new Tree(endX, endY, bLength*random(0.6, 0.8), angle + random(PI/15, PI/5));
        }
      }
      void draw(){ // Draws this Tree.
        // Draw the MAIN branch
        strokeWeight(map(blength, height/4, 3, 20, 1));
        stroke(treeColor);
        line(beginx, beginy, endX, endY);
        // Draw the OTHER branches (if they exist!).
        if(l_branch != null) l_branch.draw();
        if(r_branch != null) r_branch.draw();
      }
    }
    
    Tree my_tree; // Creates a space for a Tree.
    
    void setup() {
      size(800, 800);
      my_tree = new Tree(width/2, height, height/5, -HALF_PI); // Puts an actual Tree object in that space.
    }
    
    void draw() {
      background(255);
      my_tree.draw();
    }
    

    Notice that the branches a Tree has are ALSO Trees, so they might also have branches, which may have even more branches, and so on. This is why your "Tree" looks like a tree!

    Also notice that ALL the values used to draw a tree are now STORED IN THE CLASS - even values that were generated with some randomness! This means that the position of EVERY SINGLE BRANCH is stored somewhere, and if you edit those stored values, the tree can move (well, you might have some trouble with moving the branches too, but I'll leave it up to you to see if you can use translate() and rotate() to fix that ;-)).

  • edited June 2017

    Thanks a lot, I understand the code :)

    I found out how to do leaves myself but can you help me a little bit with the movement of the tree?

  • Draw it better, and draw it based on slightly changed values.

    class Tree {
      float l, a;
      color treeColor = color(41, 25, 3);
      Tree l_branch;
      Tree r_branch;
      Tree (float il, float ia) {
        l = il;
        a = ia;
        if (l > 3)
        {
          if (random(1) > 0.1) l_branch = new Tree(l*random(0.6, 0.8), random(-PI/5, PI/5));
          if (random(1) > 0.1) r_branch = new Tree(l*random(0.6, 0.8), random(-PI/5, PI/5));
        }
      }
      void draw() {
        strokeWeight(map(l, height/4, 3, 20, 1));
        stroke(treeColor);
        rotate(a+map(abs(-3000+millis()%6000),0,3000,-PI/50,PI/50));
        line(0, 0, l+map(abs(-7000+millis()%14000),0,7000,0,10), 0);
        translate(l+map(abs(-7000+millis()%14000),0,7000,0,10), 0);
        pushMatrix();
        if (l_branch != null) l_branch.draw();
        popMatrix();
        if (r_branch != null) r_branch.draw();
      }
    }
    
    Tree my_tree;
    
    void setup() {
      size(800, 800);  
      my_tree = new Tree(height/5, -HALF_PI);
    }
    
    void draw() {
      background(255);
      translate(width/2, height);
      my_tree.draw();
    }
    
Sign In or Register to comment.