Fibonacci Wallpaper

By Chris Dewhurst

Originally published in EUG #45

Fibonacci Wallpaper is a simple program that produces impressive patterns. It relates to the Fibonacci sequence, which evolved from adding the previous two numbers to obtain the next, normally starting with 1 and 1:

  1,1,2,3,5,8,13,21,34,55,89,144 ...

What the program does is to reduce each term to a digit sum, and use them to choose a colour to plot a several points on the screen. They are then copied to fill the whole screen, to produce patterns which are similar - but never quite the same.

The two numbers that start the Fibonacci sequence, k1 and k2, are initially set to 1 and 1 in line 80. Successive terms are calculated each time in line 170, using FNdigsum(X%) which reduces the number X% to a digit sum. A digit sum is the result of adding up the digits of a number until you get a single digit, for example 15 = 1+6 = 7. The Fibonacci sequence reduced to digit sums begins like so:

  1,1,2,3,5,8,4,7,1,8,9,8,8 ...

These are then used as indexes into the array rules. It's filled with random numbers in the range 0-3, corresponding to the 4 colours in Mode 5.

24 pixels are plotted in a square at the top left of the screen. These make up 6 bytes (there are 4 bytes per pixel in Mode 5) of screen memory, and the machine code - assembled in PROCassemb - is called to copy the bytes over the whole screen. Press any key to create another pattern.

You could try altering line 200:

  200 PLOT 69,(I DIV 6)*8,1023-(I MOD 6)*4

to plot the pixels vertically, and change the number after the ADC instruction in line 460. For example, ADC #5 will copy only 5 pixels.

There are any number of uses for Fibonacci wallpaper. You could use it to create backgrounds in a card game, or perhaps as the basis for bricks and shapes in an arcade adventure. Have fun!

Christopher Dewhurst, EUG #45