This article provides the listing for a simple computer program to generate the Sierpinski triangle using an algorithm devised by Michael Barnsley (Fractals Everywhere, Elsevier, 1988), which he coined The Chaos Game.
The program is written in Visual Basic for Applications and is easy to copy and paste straight into an Access 2007 report module. The report is then run using Print Preview to generate the pattern as a series of dots, ready for printing.
Preliminaries - Access 2007 Security Options
Before the program can be copied into Access 2007, the options button (a very visible square button below the menus) must be clicked. When the security dialog shows, select the "Enable this Content" radio button and click OK. A new report must be opened in design view and then perform the following steps as illustrated in Fig 1:
- Widen the Detail section of the report
- Click on the Detail section, the properties box will show
- Look for the OnPrint property and click on the event procedure ellipsis
- Enter the following code in the report's module
Program Code in Access 2007 VBA
The following program code may be copied and pasted straight into the VBA report module's OnPrint event procedure, as shown in Fig 2:
Print Preview - The Sierpinski Fractal Displayed
After the code is inserted in the report's OnPrint module, switch to Print Preview. This will run the program. If there are any bugs in the program steps, the Access 2007 VBA compiler will stop automatically and wait for errors to be fixed.
Figure 3 shows the pattern to expect from the program. Note, the number of iterations in the program has been set at 500,000. Each time the report is zoomed, or the screen needs to be redrawn, the program will take time to rerun (1-5 seconds depending on the speed of the computer).
Experimenting with the VBA Code
By opening the code module, one has direct access to the code's variables, which can be changed to experiment with the Sierpinski triangle. There are not many parameters, but some to try to change could be:
- colors
- iteration number
- width and height of the pattern
- initial starting point
References:
- "Fractal Market Analysis", Edgar E. Peters, Wiley & Sons NY, 1994