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
 
 
 
 
 
TitleDraw a Mandelbrot set with smoothly shaded colors in Visual Basic .NET
DescriptionThis example shows how to draw a Mandelbrot set with smoothly shaded colors in Visual Basic .NET.
Keywordsgraphics, fractals, Mandelbrot, Mandelbrot set, algorithms, complex, complex numbers, iterated system, iteration, Visual Basic .NET, VB.NET
CategoriesGraphics, Algorithms, Graphics
 

You may not have heard that Benoît Mandelbrot (November 20, 1924 - October 14, 2010) recently passed away. In his honor, I decided to enhance the Mandelbrot set program shown in an earlier example. For more information on Benoît Mandelbrot, see Wikipedia or his New York Times obituary.

The example Use a Complex class to draw the Mandelbrot set easily in Visual Basic .NET explains how to draw a Mandelbrot set by iterating the equation:

    Zn = Zn-12 + C

Where Zn and C are complex numbers. The program iterates this equation until the magnitude of Zn is at least 2 or the program performs a maximum number of iterations. At that point it uses the number of iterations to determine a color for the pixel. If the program performed num_iterations iterations and is using num_colors colors, then the program colors the pixel Colors[num_iterations % num_colors].

This example modifies the coloring algorithm to produce smoothly varying colors. First note that the following value mu approximates the fractional number of iterations that would be needed before the magnitude of Zn is at least 2.

    mu = iteration + 1 - Math.Log(Math.Log(Z.Magnitude)) / log_escape

Here iteration is the number of iterations actually performed, Z.Magnitude is the magnitude of Z right after the magnitude is greater than 2, and log_escape is the logarithm of the escape radius 2.

This value only approximates the actual expected fractional number of iterations and there is a noticeable error where the colors don't blend smoothly. Fortunately it's easy to reduce the error by using a later value of Zn. For example, if you use Zn+3, then the error isn't noticeable. The following shows the code used by the program to calculate mu.

 
' Reduce the error in mu.
For i As Integer = 0 To 2
    Z = Z * Z + C
    iteration += 1
Next i
Dim mu As Double = iteration + 1 - _
    Math.Log(Math.Log(Z.Magnitude)) / log_escape
 
This program also provides one other smooth color model. If you use the second smooth model in the configuration form, the program scales the resulting value of mu so it varies only once over the available colors as the number of iterations varies from 0 to the maximum number of iterations. That means the colors do not repeat and you get a very gradual smoothing.
 
If (ColorStyle = ColorStyles.Smooth2) Then
    mu = mu / MaxIterations * Colors.Count
End If
 
Finally after calculating mu with whichever method, the code calls GetColor to return an appropriate color for the pixel.
 
' Get a color for this pixel.
Private Function GetColor(ByVal mu As Double) As Color
    Dim clr1 As Integer = CInt(Int(mu))
    Dim t2 As Double = mu - clr1
    Dim t1 As Double = 1 - t2
    clr1 = clr1 Mod Colors.Count
    Dim clr2 As Integer = (clr1 + 1) Mod Colors.Count

    Dim r As Byte = CByte(Colors(clr1).R * t1 + _
        Colors(clr2).R * t2)
    Dim g As Byte = CByte(Colors(clr1).G * t1 + _
        Colors(clr2).G * t2)
    Dim b As Byte = CByte(Colors(clr1).B * t1 + _
        Colors(clr2).B * t2)

    Return Color.FromArgb(255, r, g, b)
End Function
 
The GetColor method truncates mu to find an integer color index value and a fractional value. It then returns a color that uses the weighted average of the integer color and the following color.

See the code for additional details.

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