Editor's Choice

A Computer Program for the Chaos Game

Using MS Access 2007 to Generate the Sierpinski Triangle

The Sierpinski Triangle - Harry P. Schlanger
The Sierpinski Triangle - Harry P. Schlanger
Copying and pasting a few program steps into an Access 2007 report and previewing, generates the self-similar Sierpinski fractal. Then it's a simple matter to experiment.

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:

Dim x As Integer, y As Integer
Dim mx As Integer, my As Integer
Me.ScaleMode = 3 'Set scale to pixels.
mx = Me.ScaleWidth / 2 ' Max value across
my = Me.ScaleHeight / 2 ' Max value height
x = 2: y = 2 ' starting point of game
For i = 1 To 500000 'number of plotted points
r = Rnd(i) 'generate a random number
If r < 0.34 Then
x = (x + mx) / 2
Else
If r < 0.67 Then
x = (x + 1) / 2
Else
x = (x + 2 * mx) / 2
End If
End If
If r < 0.34 Then
y = (y + 1) / 2
Else
y = (y + my) / 2
End If
Me.PSet (x, y) 'plot the points
Next i

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:

  1. "Fractal Market Analysis", Edgar E. Peters, Wiley & Sons NY, 1994
Harry Schlanger, Taken at work

Harry P. Schlanger - Hello, I started out as a physicist working for research organisations. Mostly in the area of heat transfer in solids and porous media. ...

rss
Advertisement
Advertisement
Advertisement