                        Leaf it out



        A computer art program for Spectrum owners

                 written by John de Rivaz



On the edge of the desert of Dessicos grow huge trees.

Their shape is sculpted by wind and other climactic [sic!]

factors. Each time the program is run a different tree is

shown.



 Theory



In New Scientist of 4 April, an article appeared on the

subject of fractals. These are shapes that repeat them-

selves 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 as 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 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 had 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 ntoes, as this program could

be heavily hacked by people interested in using the princi-

ple 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. [ Using the

         printer buffer for this is not a good idea if your

         program creates displays which its user might want

         to COPY... I've changed this to use the unused

         system variable at 23728, instead - RLB ]

60       Clears space for storing data.

70       Initialises end and start of data area and end

         point of actual data. Fct is the variable indi-

         cating 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 num-

         bers 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. [ The

         code gets re-poked every time the program is run,

         so it can safely stay in the printer buffer. ]

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.

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 every-

         thing 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.

620      Subroutine to delete branch. Imagine all 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 easily be done 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 required, and this is poked.