Utilizing Paint Event to Draw a Fractal

sharpnova

Registered User.
Local time
Today, 13:48
Joined
Jun 9, 2011
Messages
69
I recently found out about the graphics functionality that can be utilized during the paint event of a report. So I wanted to stress test it. I realize this is far outside the realm of what Access was designed for, but I'm still getting some weird behavior that I'd like to fix if possible.

Plotting a Mandelbrot is fairly simple. (given a complex c, and a complex series Z, with Z_0 = 0, and Z_(n+1) = (Z_n)^2 + c), iff Z diverges, c is not in the set.

The result of plotting all values that lie within the set is the famous Mandelbrot fractal.

The code below seems to work to a degree. It has a roughly Mandelbrot'ish shape. But it seems to be different overall.

I've checked and rechecked my math and all I can think is that there is some bug or harsh limitation in VBA.

Here is the code:

Code:
Option Compare Database

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
    'parameter declarations
    Dim width As Integer        'the width (in pixels) of the draw field
    Dim height As Integer       'the height (in pixels) of the draw field
    Dim checkDepth As Integer   'the amount of iterations of the mandelbrot formula used to determine if point diverges
    
    'parameter initializations
    width = 2700
    height = 1800
    checkDepth = 20
    
    'draw borders
    Me.Line (0, 0)-(width, 0)
    Me.Line (width, 0)-(width, height)
    Me.Line (width, height)-(0, height)
    Me.Line (0, height)-(0, 0)
    
    'z series
    Dim z_REAL As Double
    Dim z_IMAG As Double

    'value to check
    Dim c_REAL As Double
    Dim c_IMAG As Double

    For x = 0 To width Step 5
        For y = 0 To height Step 5
            z_REAL = 0
            z_IMAG = 0

            'set c_REAL and c_IMAG to a mapping of (x,y) to ([-2,2],[-2,2])
            c_REAL = 3 * (x / width) - 2
            c_IMAG = 1 - 2 * (y / height)

            For i = 1 To checkDepth
                'multiply z by itself
                z_REAL = z_REAL * z_REAL - z_IMAG * z_IMAG
                z_IMAG = 2 * z_REAL * z_IMAG
                'add c to z
                z_REAL = z_REAL + c_REAL
                z_IMAG = z_IMAG + c_IMAG

                If Abs(z_REAL) > 2 Or Abs(z_IMAG) > 2 Then Exit For

                If i = checkDepth Then
                    Me.PSet (x, y)
                End If
            Next i
        Next
    Next
End Sub

Any thoughts on why this looks so different from how a Mandelbrot should?
 
Last edited:
Figured it out.

Code:
z_REAL = z_REAL * z_REAL - z_IMAG * z_IMAG
z_IMAG = 2 * z_REAL * z_IMAG

I need to use a temporary variable here, obviously.
 
In case anyone is interested in testing this out, here is the finished code.

This works in Access 2003.

Code:
Option Compare Database

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
For i = 0 To 2700 Step 5
    For j = 0 To 1800 Step 5
        x0 = 3 * (i / 2700) - 2
        y0 = 1 - 2 * (j / 1800)
        x = 0
        y = 0
        iteration = 0
        max_iteration = 100

        While (x * x + y * y < 4) And (iteration < max_iteration)
            xtemp = x * x - y * y + x0
            y = 2 * x * y + y0
            x = xtemp
            iteration = iteration + 1
        Wend

        If iteration = max_iteration Then Me.PSet (i, j)
    Next
Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom