We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Answers
edit post, highlight code, pess ctrl-o to format your code...
move the tree building code to the Tree constructor.
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.
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:
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 ;-)).
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.