Visual Basic 2010 (Windows) Guide
Fractals: Basic Application

It will help you greatly to have read the explanations on the previous few pages. On this page you will find the instructions for making an application that plots the image of the Mandelbrot Set that you saw on the previous page. Subsequent pages cover the development of a feature to allow the user to zoom in on various sections of the original image and how to colour the images.

The basic application looks like the screenshot below,

basic application

The fractal image will be drawn in a picture box called picMandel. The picture box is 640 x 640 pixels in size and its location is 12, 27. The form was named frmMandelbrot. A StatusStrip called statusStrip1 was added to the bottom of the form. It has a ProgrssBar and a StatusLabel added to the strip - you do this on the strip itself at the bottom of the form. Lastly, there is a MenuStrip at the top of the form.

Start by creating a new Windows Application project and setting up the controls with the names given above.

The status strip will show a progress bar as the image is being drawn. The label will be used to show the value of c at any point on the graph.

The menu is not really required at this stage but will be useful as you add all sorts of extras to the application later on.

Global Variables

The following global variables are needed and are placed at the top of the form's code window,

Dim bmpMandel As New Bitmap(640, 640)
Dim unitsPerPixel As Double = 0.0
Dim minA As Double = -2.0
Dim maxA As Double = 2.0
Dim minB As Double = -2.0
Dim maxB As Double = 2.0
Dim maxIterations As Integer = 500

The bitmap will be used to make the image. The unitsPerPixel will be calculated each time an image is drawn and will be used to convert pixel values to the values of c we are plotting. minA, maxA, minB, maxB represent the range of values of the real part of the complex number c (minA, maxA) and the range of the imaginary part of c (minB, maxB). Finally, maxIterations determines how many times we iterate the Mandelbrot algorithm before deciding whether or not a value of c is in the set. Changing this number will have an effect on the way the image looks.

Mandelbrot Function

This baby is what it's all about. The function has 3 parameters, a, b and maxit. The first 2 parameters represent the real and imaginary coefficients for the complex number c. The last parameter is the maximum number of iterations allowed.

Public Function IsInMandelbrotSet(ByVal a As Double, ByVal b As Double, ByVal maxit As Integer) As Integer
   Dim squareOfDistance As Double
   Dim aSquare As Double = 0.0
   Dim bSquare As Double = 0.0
   Dim startA As Double = a
   Dim startB As Double = b
   Dim numIterations As Integer = 0
   While numIterations < maxit
      aSquare = a * a
      bSquare = b * b
      squareOfDistance = aSquare + bSquare
      If squareOfDistance > 4 Then
         Return numIterations
      End If
      b = 2 * a * b + startB
      a = (aSquare - bSquare) + startA
      numIterations += 1
   End While
   Return numIterations
End Function

The variable squareOfDistance will be used to keep track of the distance of the complex number z from the origin (0,0) on each iteration. If you look back at the diagram on the Complex Numbers page, you will see that you can make a triangle between the points (0,0), (x,y) and (x,0). The distance from (0,0) to (x,y) is the square root of the sum of the squares of the other two sides. In our program we won't actually take the square root of the value - the program runs quicker if we just check that it is less than 4.

So, what is happening here. We are using an initial value of z equal to 0, so the result of the first iteration of z2 + c is going to be c. We check that this value isn't already too large by working out the square of the real and imaginary parts (a and b). If this number is greater than 4, we return the number of iterations. If not we use these new values to make the complex number z for the first iteration.

After the if statement, the next two lines are performing the operation, z → z2 + c. A few explanations might help here. Firstly, multiplying a complex number a + bi by itself results in the following,

(a + bi)(a + bi)
a(a + bi) + bi(a + bi)
a2 + abi + abi + b2i2
a2 + 2abi + b2i2

Since i2 = -1,

a2 + 2abi - b2

If we separate into real and imaginary parts we get,

Real: a2 - b2
Imaginary: 2ab

That covers the z2 part. We add on startA to the real part since that is the real part of the complex number c. startB is imaginary part of the complex number c and is added to the new value of b.

Creating The Image

This procedure generates the image.

Public Sub CreateMandelbrotImage()
   bmpMandel = New Bitmap(640, 640)
   unitsPerPixel = (maxA - minA) / bmpMandel.Width
   Dim numIterations As Integer = 0
   Dim pixelC As New Color()
   Dim grey As Integer = 0
   Dim g As Graphics = Graphics.FromImage(bmpMandel)
   g.Clear(Color.White)
   g.Dispose()
   Cursor = Cursors.WaitCursor
   toolStripProgressBar1.Visible = True
   toolStripProgressBar1.Maximum = bmpMandel.Height
   For y As Integer = 0 To bmpMandel.Height - 1
      For x As Integer = 0 To bmpMandel.Width - 1
         Dim a As Double = minA + (x * unitsPerPixel)
         Dim b As Double = maxB - (y * unitsPerPixel)
         numIterations = IsInMandelbrotSet(a, b, maxIterations)
         If numIterations = maxIterations Then
            ' is in set - colour black
            bmpMandel.SetPixel(x, y, Color.Black)
         Else
            'not in set - colour greyscale
            grey = CInt(255 * (CDbl(maxIterations - numIterations) / CDbl(maxIterations)))
            pixelC = Color.FromArgb(255, grey, grey, grey)
            bmpMandel.SetPixel(x, y, pixelC)
         End If
      Next
      toolStripProgressBar1.Value = y
      If (y Mod 20) = 0 Then
         picMandel.Image = bmpMandel
         Application.DoEvents()
      End If
   Next
   picMandel.Image = bmpMandel
   toolStripProgressBar1.Visible = False
   Cursor = Cursors.[Default]
End Sub

The first 11 lines are used to get ready to draw the image. The bitmap is cleared, the scale is calculated, the cursor changed, and the progress bar set up.

We loop through each pixel in the picture box working through a row at a time. We use the unitsPerPixel value to convert the pixel to values of c that we want to plot. We check if the number is in the Mandelbrot set and colour it black if it is. If it is not, we colour it a shade of grey according to the number of iterations that it took before the distance got too large (more iterations = darker grey).

Every 20 rows we update the image. This shows the user what is happening. The statement Application.DoEvents() allows the program to refresh the picture box before performing the next iteration.

At the very end we make the picture box have the image we have just created and tidy up a little.

You can test the program at this stage. Add a click event handler for the a menu item, Plot. Add a call to this procedure there - CreateMandelbrotImage()

Displaying c In The StatusStrip

A little information that will come in useful when trying to make pretty pictures is to know the values of c you are working with. This allows us to 'zoom' in on various sections of the image to find the interesting stuff.

We will put this information into the label that we created on the statusstrip. The label will show the value of c for the point where the mouse is.

Private Sub picMandel_MouseMove(sender As Object, e As MouseEventArgs)
   Dim a As String = System.Convert.ToString((e.X * unitsPerPixel) + minA)
   Dim b As String = System.Convert.ToString(((bmpMandel.Height - e.Y) * unitsPerPixel) + minB)
   Dim coords As String = "(" + a + ", " + b + ")"
   toolStripStatusLabel1.Text = coords
End Sub

Notice that we use the unitsPerPixel variable to report the value of c that was used.

Having A Play

You can make this application zoom in by changing the values we gave to minA, maxA, minB, maxB. Be careful, this program is based on a square image. The same scale is used to convert pixels to values on both x and y axis. You can mess about with these values as long as the following is true of the numbers you choose,

maxA - minA = maxB - minB

Try substituting the following values into the global variable declarations,

Dim minA As Double = -0.4
Dim maxA As Double = -0.3
Dim minB As Double = 0.6
Dim maxB As Double = 0.7