Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleExplore the Mandelbrot set in VB .NET
DescriptionThis example shows how to explore the Mandelbrot set in VB .NET.
KeywordsMandelbrot, VB.NET
CategoriesGraphics, Algorithms, VB.NET
 
This example performs a couple chores such as letting the user select an area using a rubberband box. The main goal, however, is drawing the Mandelbrot set.

Subroutine AdjustAspect asjusts the world coordinates to fit the form. The program draws the Mandelbrot set in the area m_Wxmin <= X <= m_Wxmax, m_Wymin <= Y <= m_Wymax. This routine modifies those values so the ratio of width to height of this area matches the ratio of the form.

 
' Adjust the aspect ratio of the selected
' coordinates so they fit the window properly.
Private Sub AdjustAspect()
    Dim want_aspect, picCanvas_aspect As Double
    Dim hgt, wid, mid As Double

    want_aspect = (m_Wymax - m_Wymin) / (m_Wxmax - m_Wxmin)
    picCanvas_aspect = picCanvas.ClientSize.Height / _
        picCanvas.ClientSize.Width
    If want_aspect > picCanvas_aspect Then
        ' The selected area is too tall and thin.
        ' Make it wider.
        wid = (m_Wymax - m_Wymin) / picCanvas_aspect
        mid = (m_Wxmin + m_Wxmax) / 2
        m_Wxmin = mid - wid / 2
        m_Wxmax = mid + wid / 2
    Else
        ' The selected area is too short and wide.
        ' Make it taller.
        hgt = (m_Wxmax - m_Wxmin) * picCanvas_aspect
        mid = (m_Wymin + m_Wymax) / 2
        m_Wymin = mid - hgt / 2
        m_Wymax = mid + hgt / 2
    End If
End Sub
 
Subroutine DrawMandelbrot draws the set. It starts by verifying that the picCanvas control has non-zero size. It then creates Bitmap and Graphics objects on which to draw.

To draw the fractal, the program calculates the following equations:

    Z(n) = Z(n-1) * Z(n-1) + C

Where Z(n), Z(n-1), and C are all complex numbers. For a point within the drawing area, the program sets C equal to the point and sets Z(0) = 0. It then computes Z(1), Z(2), ...

It can be shown that if the magnitude of this value ever exceeds 2, then the value eventually tends towards infinity. The program continues the calculation until the magnitude squared exceeds 4 or it has reached a set maximum number of iterations. At that point, the program assigns the pixel a color that depends on the number of iterations it calculated before stopping. In other words, what was n when Z(n) got too big?

 
' Draw the Mandelbrot set.
Private Sub DrawMandelbrot()
    ' Do nothing if the canvas has zero size.
    If picCanvas.ClientSize.Width < 1 Or _
       picCanvas.ClientSize.Height < 1 Then Exit Sub

    ' Create the Bitmap and Graphic objects.
    If Not (m_Graphics Is Nothing) Then
        picCanvas.Image = Nothing
        m_Graphics.Dispose()
        m_Bitmap.Dispose()
    End If
    m_Bitmap = New Bitmap(picCanvas.ClientSize.Width, _
        picCanvas.ClientSize.Height)
    m_Graphics = Graphics.FromImage(m_Bitmap)

    ' Clear the bitmap.
    m_Graphics.Clear(Color.Black)
    picCanvas.Image = m_Bitmap
    picCanvas.Refresh()

    ' Work until the magnitude squared > 4.
    Const MAX_MAG_SQUARED As Double = 4.0

    ' Adjust the coordinate bounds to fit picCanvas.
    AdjustAspect()

    Dim clr As Integer
    Dim ReaC, ImaC, dReaC, dImaC, ReaZ, ImaZ, ReaZ2, ImaZ2 _
        As Double

    ' dReaC is the change in the real part
    ' (X value) for C. dImaC is the change in the
    ' imaginary part (Y value).
    Dim wid As Integer = picCanvas.ClientSize.Width
    Dim hgt As Integer = picCanvas.ClientSize.Height
    dReaC = (m_Wxmax - m_Wxmin) / (wid - 1)
    dImaC = (m_Wymax - m_Wymin) / (hgt - 1)

    ' Calculate the values.
    ReaC = m_Wxmin
    For i As Integer = 0 To wid - 1
        ImaC = m_Wymin
        For j As Integer = 0 To hgt - 1
            ReaZ = 0
            ImaZ = 0
            ReaZ2 = 0
            ImaZ2 = 0
            clr = 0
            Do While clr < MaxIterations And _
                    ReaZ2 + ImaZ2 < MAX_MAG_SQUARED
                ' Calculate Z(clr).
                ReaZ2 = ReaZ * ReaZ
                ImaZ2 = ImaZ * ImaZ
                ImaZ = 2 * ImaZ * ReaZ + ImaC
                ReaZ = ReaZ2 - ImaZ2 + ReaC
                clr = clr + 1
            Loop

            ' Set the pixel's color.
            m_Bitmap.SetPixel(i, j, _
                m_Colors(clr Mod m_NumColors))

            ' Move to the next point.
            ImaC = ImaC + dImaC
        Next j
        ReaC = ReaC + dReaC

        ' Let the user know we're not dead.
        If i Mod 10 = 0 Then picCanvas.Refresh()
    Next i

    ' Update the image.
    picCanvas.Refresh()

    Me.Text = "Mandelbrot (" & _
        Format$(m_Wxmin) & ", " & _
        Format$(m_Wymin) & ")-(" & _
        Format$(m_Wxmax) & ", " & _
        Format$(m_Wymax) & ")"
End Sub
 
For more information on the Mandelbrot set and other fractals, see a book on fractals. My book Visual Basic Graphics Programming, Second Edition includes VB6 programs for drawing the Mandelbrot set and other interesting fractals.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated