1# Fractal Tree Tutorial
  2
  3<!-- TOC -->
  4
  5### Start with a Tree Trunk
  6
  7Firstoff, include the BOSL2 library, then make a starting module that just has a tapered cylinder for the tree trunk.
  8
  9```openscad-3D
 10include <BOSL2/std.scad>
 11module tree(l=1500, sc=0.7)
 12    cylinder(h=l, d1=l/5, d2=l/5*sc);
 13tree();
 14```
 15
 16### Attaching a Branch
 17
 18You can attach a branch to the top of the trunk by using `attach()` as a child of the trunk cylinder.
 19
 20```openscad-3D
 21include <BOSL2/std.scad>
 22module tree(l=1500, sc=0.7)
 23    cylinder(h=l, d1=l/5, d2=l/5*sc)
 24        attach(TOP)
 25            yrot(30) cylinder(h=l*sc, d1=l/5*sc, d2=l/5*sc*sc);
 26tree();
 27```
 28
 29### Replicating the Branch
 30
 31Instead of attaching each branch individually, you can make multiple copies of one branch, that are rotated relative to each other.
 32
 33```openscad-3D
 34include <BOSL2/std.scad>
 35module tree(l=1500, sc=0.7)
 36    cylinder(h=l, d1=l/5, d2=l/5*sc)
 37        attach(TOP)
 38            zrot_copies(n=2)  // Replicate that branch
 39                yrot(30) cylinder(h=l*sc, d1=l/5*sc, d2=l/5*sc*sc);
 40tree();
 41```
 42
 43### Use Recursion
 44
 45Since branches look much like the main trunk, we can make the tree recursive. Don't forget the termination clause, or else it'll try to recurse forever!
 46
 47```openscad-Med
 48include <BOSL2/std.scad>
 49module tree(l=1500, sc=0.7, depth=10)
 50    cylinder(h=l, d1=l/5, d2=l/5*sc)
 51        attach(TOP)
 52            if (depth>0)  { // Important!
 53                zrot_copies(n=2)
 54                yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
 55            }
 56tree();
 57```
 58
 59### Make it Not Flat
 60
 61A flat planar tree isn't what we want, so lets bush it out a bit by rotating each level 90 degrees.
 62
 63```openscad-Med
 64include <BOSL2/std.scad>
 65module tree(l=1500, sc=0.7, depth=10)
 66    cylinder(h=l, d1=l/5, d2=l/5*sc)
 67        attach(TOP)
 68            if (depth>0) {
 69                zrot(90)  // Bush it out
 70                zrot_copies(n=2)
 71                yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
 72            }
 73tree();
 74```
 75
 76### Adding Leaves
 77
 78Let's add leaves. They look much like squashed versions of the standard teardrop() module, so lets use that.
 79
 80```openscad-Big
 81include <BOSL2/std.scad>
 82module tree(l=1500, sc=0.7, depth=10)
 83    cylinder(h=l, d1=l/5, d2=l/5*sc)
 84        attach(TOP)
 85            if (depth>0) {
 86                zrot(90)
 87                zrot_copies(n=2)
 88                yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
 89            } else {
 90                yscale(0.67)
 91                teardrop(d=l*3, l=1, anchor=BOT, spin=90);
 92            }
 93tree();
 94```
 95
 96### Adding Color
 97
 98We can finish this off with some color. The `color()` module will force all it's children and
 99their descendants to the new color, even if they were colored before. The `recolor()` module,
100however, will only color children and decendants that don't already have a color set by a more
101nested `recolor()`.
102
103```openscad-Big
104include <BOSL2/std.scad>
105module tree(l=1500, sc=0.7, depth=10)
106    recolor("lightgray")
107    cylinder(h=l, d1=l/5, d2=l/5*sc)
108        attach(TOP)
109            if (depth>0) {
110                zrot(90)
111                zrot_copies(n=2)
112                yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
113            } else {
114                recolor("springgreen")
115                yscale(0.67)
116                teardrop(d=l*3, l=1, anchor=BOT, spin=90);
117            }
118tree();
119```
120
121