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
 
 
 
 
 
 
TitleDraw a fractal Mandelbrot set in VB.NET
DescriptionThis example shows how to draw a fractal Mandelbrot set in VB.NET.
Keywordsfractal, Mandelbrot, Mandelbrot set
CategoriesGraphics, VB.NET
 
The Mandelbrot set uses an iterated equation to calculate colors for the points in a region. The equation is:

    Z(n) = Z(n-1)^2 + C

Here the Z(n) and C are complex numbers.

It can be shown that if the magnitude of Z(n) ever exceeds 2, then it eventually diverges towards infinity.

To find the color for the point (x, y), the program sets Z(0) = 0 and C = x + y * i. It then generates values for Z(n) until Z(n)'s magnitude exceeds 2 or it reaches some predetermined maximum number of iterations. At that point, the program uses the number of iterations it performed to assign the point's color. For example, if the program is using K colors and it performed I iterations, then it assigns the point color number I Mod K.

The following code shows how the program draws the Mandelbrot set.

 
' Draw the Mandelbrot set.
Private Sub DrawMandelbrot()
    ' Work until the magnitude squared > 4.
    Const MAX_MAG_SQUARED As Integer = 4

    Dim wid As Integer
    Dim hgt As Integer
    Dim clr As Integer
    Dim X As Integer
    Dim Y As Integer
    Dim ReaC As Double
    Dim ImaC As Double
    Dim dReaC As Double
    Dim dImaC As Double
    Dim ReaZ As Double
    Dim ImaZ As Double
    Dim ReaZ2 As Double
    Dim ImaZ2 As Double

    ' Make a Bitmap to draw on.
    m_Bm = New Bitmap(picCanvas.ClientSize.Width, _
        picCanvas.ClientSize.Height)
    Dim gr As Graphics = Graphics.FromImage(m_Bm)

    ' Clear.
    gr.Clear(picCanvas.BackColor)
    picCanvas.Image = m_Bm
    Application.DoEvents()

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

    ' dReaC is the change in the real part
    ' (X value) for C. dImaC is the change in the
    ' imaginary part (Y value).
    wid = picCanvas.ClientRectangle.Width
    hgt = picCanvas.ClientRectangle.Height
    dReaC = (m_Xmax - m_Xmin) / (wid - 1)
    dImaC = (m_Ymax - m_Ymin) / (hgt - 1)

    ' Calculate the values.
    ReaC = m_Xmin
    For X = 0 To wid - 1
        ImaC = m_Ymin
        For Y = 0 To hgt - 1
            ReaZ = Zr
            ImaZ = Zim
            ReaZ2 = Z2r
            ImaZ2 = Z2im
            clr = 1
            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 value.
            m_Bm.SetPixel(X, Y, m_Colors(clr Mod NumColors))

            ImaC = ImaC + dImaC
        Next Y
        ReaC = ReaC + dReaC

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

    Text = "Mandelbrot (" & _
        m_Xmin.ToString("0.000000") & ", " & _
        m_Ymin.ToString("0.000000") & ")-(" & _
        m_Xmax.ToString("0.000000") & ", " & _
        m_Ymax.ToString("0.000000") & ")"
End Sub
 
The program allows you to zoom in on areas of the set and to pick the color palette it uses to draw. If you resize the program or change the colors, use the Scale menu's Refresh command to redraw the fractal.

For more information on fractals, including information about the fascinating Julia set that uses the Mandelbrot set as a map, see my book Visual Basic Graphics Programming.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated