FRACTAL TREE
by Juan de Rivaz
from Popular Computing Weekly, 3-9 October 1985

Leaf it out

A computer art program for Spectrum owners written by Juan de Rivaz


On the edge of the desert of Dessicos grow huge trees. Their shape is
sculpted by wind and other climatic factors. Each time the program is run a
different tree is shown.

Theory

In New Scientist of 4th April an article appeared on the subject of fractals.
These are shapes that repeat themselves at many different sizes. For example,
a coastline has smaller bays and promontories that cannot be seen at a
distance, but as you approach they are seen to have a similar shape to the
large features.

This program was written on the Sinclair Spectrum to produce fractal trees.
There are variables for wind and climate, which control the angle and lengths
of the branches. To produce a perfectly regular tree one can set these to
zero. In addition, other variables can be altered to set up the initial
conditions to specify different shapes of tree.

The program basically draws a trunk and then two branches from it. From the
ends of these branches it draws two more until all the memory is filled with
the starting points for branches.

In order to provide for as complex a tree as possible, branch conditions are
stored as POKEs in RAM, and once drawn details of a branch are deleted. This
makes the program more complicated, as machine code has to be used to delete
the data POKEd into RAM. Using the Spectrum's string handling took up too
much memory. A LDIR has to be used to delete the used branch as opposed to a
FOR/NEXT loop with PEEKs and POKEs, because the latter would have taken too
long to execute.

I include detailed program notes, as this program could be heavily hacked by
people interested in using the principle to make other drawings. This
approach to computer art enables people like me, who can't draw, to produce
pictures using computational power as opposed to merely composing a picture
by setting bits in screen RAM.


Program notes

Line No.
  10-110   Set up the machine code and memory
  30       Sees whether program has been run before, and if so does not
           attempt to read RAMTOP again.
  40       Stores RAMTOP in printer buffer.
  60       Clears space for storing data.
  70       Initialises end and start of data area and end point of actual
           data. FCT is the variable indicating the number of branches. It
           is set at 0.5 because it is doubled at the start of the main loop.
  80-100   Read in machine code. It switches off interrupts and loads
           registers for the LDIR. The actual numbers for the registers are
           POKEd in by BASIC to make it easier to alter the amount of memory
           space when modifying the program. See REM line 50.
 110       POKEs in the numbers for the registers.
 120-150   Makes a blue sky and sandy backdrop for the tree. As green leaves
           can be printed a green backdrop would be inappropriate.
 160-290   Set up input variables.
 320-610   Main loop.
 360       If all the memory is used up F$ is a space, not the null string,
           and the fractal counter doesn't increase and all the remaining data
           is used up.
 370       Inputs the random factors and also the shape reduction factors RF
           and WF. The wind and climate are printed #1 not 1; ie. on the
           lower part of the screen.
 380       Each set has FCT branches and this loop draws each one and creates
           the parameters for the next generation.
 390-470   This draws a branch, rejecting any which would go off-screen. Such
           branches do not produce others and are deleted undrawn.
 420       This line draws lengths less than two in green, so that they may
           appear as leaves. Alternatively it could be replaced with a line
           that draws everything with FCT greater than a certain number in
           green.
 480-610   This section creates the parameters for new branches.
 500       Tests to see whether the space is used up and skips section if it
           has been.
 620       Subroutine to delete branch. Imagine all the data in memory
           stretching from ST to EP, where EP>ST. All one needs to do is to
           load ST with ST+3 and so on to EP. This can be done easily in
           BASIC; unfortunately this is quick to write but slow in execution.
 670       In the machine code section the length of the block to be moved
           is calculated and this is POKEd.
9020       Microdrive SAVE routine.