We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm trying to make a program that in essence makes a tree by having a branching effect, and want the strokeweight to decrease with each "generation" of branch (so the "trunk" appears thickest, and each branch is smaller in its width), any suggestions? Here's what I have thus far: (apologies for the obvious comments, we're supposed to include them for a class I'm taking)
float angle; //declares the variable angle as a float value
void setup(){ size(840, 640); strokeWeight(1); background(255); } /makes background white, sets size of screen, makes strokeweight 1/
void draw(){ translate(width/2, height); /makes the origin the middle bottom of screen/ if (mousePressed) { branches(150);} /draws initial branch of x length 150, requires the mouse to be pressed to start the tree drawing process/
if (mousePressed){ if (mouseButton == RIGHT){ background(255);}} /this is so when you right click the mouse, whatever tree was previously there will be covered by the background to give a fresh slate/ }
void branches(float x){ //this will draw the branches, where x is the branch length
float angle = random(0, PI/4); //makes the angle that the branches make with previous
line(0, 0, 0, -x); //makes line from middle bottom of screen up with length x
translate(0, -x); //this will put the origin to the end of the line that was just made
x = 0.75; //this is to make every subsequent branch 3/4 the size of the former
if (x > 2){ /There were problems with the pushMatrix running too many times, so this if statement was
added to limit the size of the pixels the branches could be,
otherwise they'd go to infinity*/
pushMatrix(); //to store the state of the new branch to the right
rotate (angle);
branches(x); //draws a branch to the right
popMatrix(); //goes back to the top of the last branch
pushMatrix(); //stores state for new branch to the left
rotate(-angle);
branches(x); //draws a branch to the left
popMatrix(); //goes back to top of last branch
}
}
Answers
Go back
Edit your old post
The code needs formatting
Before and after the code 2 empty lines
Then select it
Then ctrl-o
strokeWeight(x);
?
You can also use map() to put x on a more intelligent range to use in strokeWeight()