Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a Complex class to draw the Mandelbrot set easily in Visual Basic .NET
DescriptionThis example shows how to use a Complex class to draw the Mandelbrot set in Visual Basic .NET.
KeywordsMandelbrot set, fractal, Visual Basic .NET, VB.NET, Visual Basic
CategoriesGraphics, Graphics, Algorithms
 

You can draw a Mandelbrot set by iterating the equation:

    Zn = Zn-12 + C

Where Zn and C are complex numbers. For a point (X, Y) in a particular area of the set, the program sets Z0 = 0 and C = X = Y * i. The program iterates this equation until the magnitude of Zn is at least 2 or the program performs a maximum number of iterations.

This example uses a Complex class to manage the complex numbers easily and intuitively. The following code shows the program's main loop.

 
Dim Z As Complex = Z0
Dim C As New Complex(ReaC, ImaC)
clr = 1
Do While clr < MaxIterations And Z.MagnitudeSquared < _
    MAX_MAG_SQUARED
    ' Calculate Z(clr).
    Z = Z * Z + C
    clr = clr + 1
Loop

' Set the pixel's value.
m_Bm.SetPixel(X, Y, m_Colors(clr Mod NumColors))
 
The rest of the main program's code (and there's a lot of it) deals with resizing the form, letting you use the mouse to select an area for zooming, and so forth.

The following code shows the parts of the Complex class that are most important to this example.

 
Public Class Complex
    Public Re As Double = 0
    Public Im As Double = 0
    ' ...

    ' Binary +.
    Public Shared Operator +(ByVal c1 As Complex, ByVal c2 _
        As Complex) As Complex
        Return New Complex(c1.Re + c2.Re, c1.Im + c2.Im)
    End Operator

    ' Binary *.
    Public Shared Operator *(ByVal c1 As Complex, ByVal c2 _
        As Complex) As Complex
        Return New Complex( _
            c1.Re * c2.Re - c1.Im * c2.Im, _
            c1.Re * c2.Im + c1.Im * c2.Re)
    End Operator

    ' ...
End Class
 
See the code for details.

Compare this example to Use a Complex class to draw the Mandelbrot set easily in Visual Basic 6. The .NET version is much faster.

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