|
|
Technology >>
Programming
>>
|
Computer Program for the Chaos GameHow to Generate the Sierpinski Triangle in MS Access
Sep 28, 2010 © Harry P. Schlanger
Copying and pasting a few program steps into an Access 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 Barnsley (1988), which he coined "
The Chaos Game".
The program is written in Visual Basic for Applications (VBA) 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.
Access 2007 OnPrint Event
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 one must perform the following steps
(see Fig.1 below):
- 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
|
|
Figure 1. Accessing the OnPrint Event Procedure
|
Access 2007 VBA - Program Code
The following program code may be copied and pasted straight into the VBA report module's OnPrint event
procedure (see 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
|
|
Figure 2. Inserting the Code into the Code Window
|
Displaying Sierpinski Triangle
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. Fractal Pattern Produced in Print Preview
|
Figure 3 above 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 - Change VBA Code Variables
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
The reader might be interested in a related article, "
Programming MSAccess Applications".
References:
- Barnsley, Michael. "Fractals Everywhere", Elsevier, 1988.
- Peters, Edgar E. "Fractal Market Analysis", Wiley & Sons NY, 1994
The copyright of the article Computer Program for the Chaos Game: How to Generate the Sierpinski Triangle in MS Access is owned by Harry P. Schlanger. Permission to republish in print or online must be granted by the author in writing.
|
|
Custom Search
Other Articles:
Website Construction:
Gum Leaf Designs © 2011
|