Branching Tree
You'll write a function that calls itself to draw a tree, add randomness to the angles, and grow a forest where no two trees match.
About
Recursion is the technique that makes a dozen lines of code draw a thousand branches, and a tree is the kindest place to learn it. The function draws one branch, then calls itself twice with a shorter length and a turn to each side, until the branches are too short to bother with. Random angles and lengths turn a diagram into something that looks grown, and a seed keeps the ones you like. The trap is forgetting the stopping condition, which freezes your browser tab, so write the exit before you write the recursion.
At a glance
- Per requirement
- 15 XP
- Completion bonus
- 75 XP
- Requirements
- 5
- Takes
- One evening
- Skill level
- Beginner (done it a few times)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Draw a symmetric tree with a function that calls itself. Each branch is a line. Push a transform, move to the tip, rotate 25 degrees, call the function with 0.67 times the length, then do the same the other way. Stop when the length drops below 4 pixels. Post a screenshot of the tree with the code. Photo
-
2
Make the angles and lengths random within a range. Replace 25 degrees with random(15, 35) and 0.67 with random(0.6, 0.75), seeded so you can get a tree back. Post four trees from four seeds in a row. Photo
-
3
Make the branches thinner as they get further from the trunk. Pass the stroke weight down and multiply it by 0.7 at each level, starting at about 12 pixels for the trunk. This one change does more for the look than anything else. Post a before and an after. Photo
-
4
Add a leaf or a flower rule at the branch tips. When the length drops below a threshold, draw a small ellipse in a second color instead of stopping. Try a rule where only some tips get a leaf, say random(1) < 0.3. Post a close-up crop of the tips. Photo
-
5
Grow a forest of at least twenty trees and give it a horizon. Place the trees along a ground line, smaller and paler toward the back so they read as distant. Export at 3000 pixels wide. Post it and write what you'd change if you had one more evening. Photo