Like many other Computer Science undergraduate students I wrote1 a program that renders images of the Mandelbrot set. Although my implementation is quite simple (a resume with most important details is listed below) it is a good working example that maybe can help others who are struggling with rendering fractals, implementing multi-threading or programming in C# in general.
It’s quite amazing how this seemingly simple calculation can generate such complex images.
/// This is the core function where the fractal magic happens. It calculates for the given coordinates in the complex plane the mandel number which is used for colouring.
/// <param name="x">Real number.</param>
/// <param name="y">Imaginairy number.</param>
/// <returns>Complex number.</returns>
private int GiveMandelNumber(double x, double y)
var mandelNumber = 0;
double a = 0, aOld = 0, b = 0, c = 0;
while (c <= 4 && mandelNumber < _iterations)
a = a * a - b * b + x;
b = 2 * aOld * b + y;
aOld = a;
c = a * a + b * b;
Details on my implementation (order is quite random)
- 7 build-in presets (at locations I really like).
- Maximum supported number of iterations 60,000 (default setting is 400).
- Anti-aliasing up to 6 times (resolution of 7,200×7,200 pixels on high-res displays).
- Image is divided in chunks for parallel computation (default chunk size is 10 so the image is then cut into 10 * 10 = 100 parts).
- A progress bar is updated which indicates the number of finished chunks.
- All calculations are performed by the CPU.
- The red, green and blue tones can be adjusted separately.
- A rendered image can be saved as PNG, Jpeg or BMP.
- Mouse drag and drop to move the rendered image.
- Double left click to zoom in and double right click to zoom out (zoomfactor is 2).
- Max level of zoom is somewhere near 5E-17.
At nearly every point there is room for improvement but I’ll leave that up to you :D. You can download the source code or the executable on Github.
1 Part of the course “Imperative Programming” at Utrecht University.