2020-03-22 08:14:26 +00:00
# Fractal Tree Tutorial
2021-04-12 07:41:12 +00:00
<!-- TOC -->
2020-03-22 08:14:26 +00:00
### Start with a Tree Trunk
2020-03-24 07:02:24 +00:00
Firstoff, include the BOSL2 library, then make a starting module that just has a tapered cylinder for the tree trunk.
2020-03-22 08:14:26 +00:00
2022-02-09 02:40:58 +00:00
```openscad-3D
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
2020-03-24 07:02:24 +00:00
module tree(l=1500, sc=0.7)
cylinder(l=l, d1=l/5, d2=l/5*sc);
tree();
2020-03-22 08:14:26 +00:00
```
2020-03-24 07:02:24 +00:00
### Attaching a Branch
2020-03-22 08:14:26 +00:00
2020-03-24 07:02:24 +00:00
You can attach a branch to the top of the trunk by using `attach()` as a child of the trunk cylinder.
2020-03-22 08:14:26 +00:00
2022-02-09 02:40:58 +00:00
```openscad-3D
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
2020-03-24 07:02:24 +00:00
module tree(l=1500, sc=0.7)
cylinder(l=l, d1=l/5, d2=l/5*sc)
attach(TOP)
yrot(30) cylinder(l=l*sc, d1=l/5*sc, d2=l/5*sc*sc);
tree();
2020-03-22 08:14:26 +00:00
```
2020-03-24 07:02:24 +00:00
### Replicating the Branch
2020-03-22 08:14:26 +00:00
2020-03-24 07:02:24 +00:00
Instead of attaching each branch individually, you can make multiple copies of one branch, that are rotated relative to each other.
2020-03-22 08:14:26 +00:00
2022-02-09 02:40:58 +00:00
```openscad-3D
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
module tree(l=1500, sc=0.7)
cylinder(l=l, d1=l/5, d2=l/5*sc)
attach(TOP)
2020-03-24 07:02:24 +00:00
zrot_copies(n=2) // Replicate that branch
yrot(30) cylinder(l=l*sc, d1=l/5*sc, d2=l/5*sc*sc);
2020-03-22 08:14:26 +00:00
tree();
```
### Use Recursion
2020-03-24 07:02:24 +00:00
Since 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!
2020-03-22 08:14:26 +00:00
2020-03-24 07:02:24 +00:00
```openscad-Med
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
module tree(l=1500, sc=0.7, depth=10)
cylinder(l=l, d1=l/5, d2=l/5*sc)
attach(TOP)
2020-03-24 07:02:24 +00:00
if (depth>0) { // Important!
2020-03-22 08:14:26 +00:00
zrot_copies(n=2)
yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
2020-03-24 07:02:24 +00:00
}
2020-03-22 08:14:26 +00:00
tree();
```
### Make it Not Flat
A flat planar tree isn't what we want, so lets bush it out a bit by rotating each level 90 degrees.
2020-03-24 07:02:24 +00:00
```openscad-Med
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
module tree(l=1500, sc=0.7, depth=10)
cylinder(l=l, d1=l/5, d2=l/5*sc)
attach(TOP)
2020-03-24 07:02:24 +00:00
if (depth>0) {
2020-03-22 08:14:26 +00:00
zrot(90) // Bush it out
zrot_copies(n=2)
yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
2020-03-24 07:02:24 +00:00
}
2020-03-22 08:14:26 +00:00
tree();
```
### Adding Leaves
Let's add leaves. They look much like squashed versions of the standard teardrop() module, so lets use that.
2020-03-24 07:02:24 +00:00
```openscad-Big
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
module tree(l=1500, sc=0.7, depth=10)
cylinder(l=l, d1=l/5, d2=l/5*sc)
attach(TOP)
2020-03-24 07:02:24 +00:00
if (depth>0) {
2020-03-22 08:14:26 +00:00
zrot(90)
zrot_copies(n=2)
yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
2020-03-24 07:02:24 +00:00
} else {
2020-03-22 08:14:26 +00:00
yscale(0.67)
teardrop(d=l*3, l=1, anchor=BOT, spin=90);
2020-03-24 07:02:24 +00:00
}
2020-03-22 08:14:26 +00:00
tree();
```
### Adding Color
We can finish this off with some color. The `color()` module will force all it's children and
their descendants to the new color, even if they were colored before. The `recolor()` module,
however, will only color children and decendants that don't already have a color set by a more
nested `recolor()` .
2020-03-24 07:02:24 +00:00
```openscad-Big
2020-03-22 08:14:26 +00:00
include < BOSL2 / std . scad >
module tree(l=1500, sc=0.7, depth=10)
recolor("lightgray")
cylinder(l=l, d1=l/5, d2=l/5*sc)
attach(TOP)
2020-03-24 07:02:24 +00:00
if (depth>0) {
2020-03-22 08:14:26 +00:00
zrot(90)
zrot_copies(n=2)
2020-03-24 07:02:24 +00:00
yrot(30) tree(depth=depth-1, l=l*sc, sc=sc);
} else {
2020-03-22 08:14:26 +00:00
recolor("springgreen")
yscale(0.67)
teardrop(d=l*3, l=1, anchor=BOT, spin=90);
2020-03-24 07:02:24 +00:00
}
2020-03-22 08:14:26 +00:00
tree();
```
2022-02-09 02:40:58 +00:00