Here is a general tree drawing function:
TO TREE :LEN
IF :LEN < 2 [STOP]
LT 45 FD :LEN
TREE :LEN / 2
BK :LEN RT 90
FD :LEN
TREE :LEN/2
BK :LEN LT 45
END
Enter TREE 50 to draw a sample tree.
Add BK 50 to draw the trunk.
We can amend this basic routine in the usual ways, for example by changing the angle of the branches or any of the other numbers in the code:
TO TREE :LEN
IF :LEN < 1 [STOP]
LT 20 FD :LEN
TREE :LEN / 1.5
BK :LEN RT 40
FD :LEN
TREE :LEN/1.5
BK :LEN LT 20
END
TREE 40
BK 50

As you can see this amended code makes the tree narrower and denser in 'foliage' at the top. This is because we are dividing by 1.5 instead of 2 so the length of each branch is reduced by less each time and there are more of them - 40/2=20, 20/2=10, 10/2=5, 5/2=2, 2/2=1, 1/2=0.5 so STOP; this gives 5 branches. How many branches if we divide by 1.5? Or divide by 1.1? Or divide by 0.5?
We can make other changes too:
TO TREE :LEN
IF :LEN < 1 [STOP]
LT 20 FD :LEN
TREE :LEN / 1.5
BK :LEN RT 60
FD :LEN
TREE :LEN/1.5
BK :LEN LT 40
END
TREE 40
BK 50
The leaning or lopsided effect has been achieved by turning 20 degrees on one side of the tree and 40 degrees on the other. Notice that the right turn is 60 degrees, the sum of the other two angles; if the right turn does not equal the sum of the two left turns the tree will not draw correctly. Compare this with a real beech tree and some conifer foliage:


Each species of tree has a natural angle at which branches will divide, though this may be changed by factors such as weather and competition from other trees.
To make our trees more realistic we can vary the thickness of the branches, making them thinner towards the ends, and also make the lines at the ends of the branches a different colour:

TO TREE :LEN :P
IF :LEN < 1 [STOP]
IF :LEN > 3 [SETPC 19]
IF :LEN < 3 [SETPC 32]
SETPENWIDTH :P
LT 20 FD :LEN
TREE :LEN / 1.5 :P-1
IF :LEN > 3 [SETPC 19]
IF :LEN < 3 [SETPC 32]
SETPENWIDTH :P
BK :LEN RT 60
FD :LEN
TREE :LEN/1.5 :P-1
IF :LEN >3 [SETPC 19]
IF :LEN <3 [SETPC 32]
BK :LEN LT 40
END
TREE 40 10
SETPENWIDTH 10
BK 50
It may be going a little far to say that we have created a formula for growing trees like some kind of computerised DNA but we have provided an insight into what Mandelbrot called 'the fractal geometry of nature'. These trees, like real trees, are fractal in that they show the same branching pattern at a number of scales, and we can control their shape by changing a few numbers. Natural trees grow in a less regular fashion as a result of local conditions such as climate and random variations but each species still has a characteristic shape and branching pattern.

Not all forms in nature are fractal but some of them are and the functions we have used provide some insight into the way complex structures can be created from relatively simple 'rules'.
You can add leaves or bring the trees into bloom by calling a simple routine at the end of each branch:
TO TREE :LEN
IF :LEN < 1 [BLOOM STOP]
LT 40 FD :LEN
TREE :LEN / 1.6
BK :LEN RT 90
FD :LEN
TREE :LEN/1.6
BK :LEN LT 50
END
TO BLOOM
SETPC 10
REPEAT 45 [FD 0.1 LT 8]
SETPC 25
END
You can change the bloom routine, adjusting the size and colour of the blossoms as you see fit and also linking it with changes in the width of the pen. You can also change the colours of the trees to produce something more like coral.

This is a real soft coral of type Dendronephthya. You will find many similar shapes among corals.

Here the code has been modified to include a variable angle:
TO TREE :LEN :A
IF :LEN < 10 [STOP]
LT :A FD :LEN
TREE :LEN / 1.8 :A
BK :LEN RT :A * 2
FD :LEN
TREE :LEN/1.8 :A
BK :LEN LT :A
END
This code is called from this function:
to calltree
cs
make "a random (160) + 20
show :a
make "b random (120)
show :b
tree :a :b
bk 200
end
This function adds a clear screen command, sets the starting length and the angle of turn to random values and adds a trunk at the end. This can be used to create trees of random variation.
We can scale the angles as follows:
TO TREE :LEN :A
IF :LEN < 10 [STOP]
LT :A * 0.5 FD :LEN
TREE :LEN / 1.8 :A
BK :LEN RT :A * 2
FD :LEN
TREE :LEN/1.8 :A
BK :LEN LT :A * 1.5
END
If :A is 60 the first turn is 60 * 0.5 = 30 and the second turn is 60 * 1.5 = 90. Together these numbers make 120 or 60 * 2.
Here is another variation:
TO TREE :LEN :A
IF :LEN < 5 [STOP]
LT :A * 0.5 FD :LEN
TREE :LEN / 1.6 :A
BK :LEN RT :A * 2
FD :LEN
TREE :LEN/1.5 :A
BK :LEN LT :A * 1.5
END
Here the lengths of the branches are reduced at differential rates so the left
and right parts are different. You now have the means to draw an infinite
variety of trees with one simple call to 'CALLTREE'. How about making a forest?
to forest
cs
pu bk 200 pd
rt 90 fd 400 lt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
lt 90 fd 100 rt 90
fd 200 calltree
end
This produces a row of trees and a suggestion of a canopy and a forest floor. Now you can colour them and set the branches to different thicknesses...
The side of the Koch snowflake can be exaggerated to produce a different family of fractal curves:
to fork :L :S
if :L = 1 [fd :S][fork :L-1 :s/2 lt 90 fork :L-1 :S/2 rt 180 fork :L-1 :S/2 lt
90 fork :L-1 :S/2]
end
repeat 4 [fork 4 100 lt 90]
This produces a rectilinear grid while the following gives a more 'crusted' pattern:
to fork :L :S
ifelse :L = 1 [fd :S]
[fork :L - 1 :S/2
lt 86
fork :L-1 :S/2
rt 172
fork :L-1 :S/2
lt 86
fork :L-1 :S/2]
end
fork 3 120
repeat 4 [fork 4 100 lt 90]
Try changing the angles to produce different patterns.
This code draws a square motif in the middle of each line:
to carpet :L :S
if :L=1 [fd :S][line :L-1 :S/2 lt 45 fd :S/3 lt 90 fd :S/3 lt 90 fd :S/3 lt 90
fd :S/3 lt 45 line :L-1 :S/2]
end
cs repeat 4 [line 4 100 lt 90]