Title: Use a Complex number class to draw a Mandelbrot set fractal easily in C#
The example Draw a Mandelbrot set fractal in C# explains how to draw a Mandelbrot set by iterating the equation:
Z(n) = Z(n-1)2 + C
Where Z(n) and C are complex numbers. The program iterates this equation until the magnitude of Z(n) is at least 2 or the program performs a maximum number of iterations.
That example tracks the numbers' real and imaginary parts in separate variables. This example uses a Complex class to manage the complex numbers more easily and intuitively. The following code shows the loop the program uses to iterate the Z(n) function.
Complex Z = Z0;
Complex C = new Complex(ReaC, ImaC);
int clr = 1;
while ((clr < MaxIterations) && (Z.MagnitudeSquared < MAX_MAG_SQUARED))
// Calculate Z(clr).
Z = Z * Z + C;
The Complex class is fairly straightforward, but it's long so it isn't shown here. Download the example program to see how it and the rest of the program works.
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. The code is in Visual Basic 6 so you'll have to do some translating but the math still works. (I've had no luck trying to get my publisher, Wiley, to let me make a .NET version of the book. If you would buy a C# version of the book, let me know.)
Download the example to experiment with it and to see additional details.