About this stage
Visually, this is the same graph as before.
However, this time, we are using a different technique for drawing the trunk branch.
Processing has a translate function that allows us to move the functional coordinate system from the upper left corner to a point we specify.
That new point becomes our 'origin' and all other measurements are directed from there.
This slight but powerful 'shift' will make creating our tree a 'snap!'
Behind the scenes:
We know you could have viewed this source code on your own, but for your convenience, here are some code snippets.
Comparing the code from Stages 1 and 2:
Stage 1:
//vertical branch
strokeWeight(2);
stroke(branchColor);
line(5*inc, 9*inc, 5*inc, 9*inc - branchLength);
if(shouldShowLeaves){
noStroke();
fill(leafColor);
circle(5*inc, 9*inc - branchLength, .25*inc);
}
Stage 2:
//vertical branch
strokeWeight(2);
stroke(branchColor);
translate(5*inc, 9*inc);
line(0, 0, 0, -branchLength);
if(shouldShowLeaves){
noStroke();
fill(leafColor);
circle(0, -branchLength, .25*inc);
}
By translating the origin to the base of the trunk branch, notice how much easier it was to draw the line and the circle in Stage 2. Remember, in this setting, as opposed to math class, vertical downward movements are positive, upward movements are negative.
Last update: 01/10/20