Skip to content
Benedict Endemann edited this page Aug 11, 2016 · 3 revisions

##Tree Demo

In this demo, we'll use our newfound library to create a tree.

Let's start with the trunk.

rod(d=20, h=100);

Now let's stick a branch on it. We'll use parent to copy the shape and size of the trunk. From there, we just scale it down some.

rod(d=20, h=100)
    align(top)
    scale([0.8, 0.8, 0.8])
    parent(anchor=bottom);

Most real trees have more than one branch, though, so let's rotate and mirror it.

rod(d=20, h=100)
    align(top)
    mirrored([1,0,0])
    rotate([0,15,0])
    scale([0.8, 0.8, 0.8])
    parent(anchor=bottom);

If we make a module, it'll real easy to add even more branches.

module branch(){
    align(top)
    mirrored([1,0,0])
    rotate([0,15,0])
    scale([0.8, 0.8, 0.8])
    parent(anchor=bottom)
    children();
}
rod(d=20, h=100)
    branch()
    branch();

Except these branches are all pointed in one direction. We don't want that, so for each iteration let's rotate the frame of reference.

module branch(){
    align(top)
    rotate([0,0,90])
    mirrored([1,0,0])
    rotate([0,15,0])
    scale([0.8, 0.8, 0.8])
    parent(anchor=bottom)
    children();
}
rod(d=20, h=100)
    branch()
    branch();

Almost done. We can make it even easier to add branches if we use recursion. Make sure to add an end condition, or you'll crash OpenSCAD!

module branch(n){
    if(n>0)
    align(top)
    rotate([0,0,90])
    mirrored([1,0,0])
    rotate([0,15,0])
    scale([0.8, 0.8, 0.8])
    parent(anchor=bottom)
    branch(n-1);
}
rod(d=20, h=100)
branch(5);

Clone this wiki locally