Skip to content


A QT Project – Fractal Generator – Mandelbrot

Yeeehaaaa! I finally managed to implement the calculator for the Mandelbrot set, First of all I implemented my on complex arithmetics. I wrote a simple class called ComplexNumber. It is defined in arthur_complex.h and implemented in arthur_complex.cpp.

With the help of operator overloads I implemented addition, subtraction, multiplication and division operations. I also implemented the calculation of the absolute value of the complex number. Actually I implemented square of it absPow2(). The abs would require square root calculation which slows the processing. I need abs only for comparing it to a boundary value, so I can compare absPow2() to the square of the boundary value. It works fine for Mandelbrot. Lets see whether we need abs later. The other thing why I did this is portability. For calculating square root I would need a math library and I should consider its portability which just adds complications.
Then I started to implement the calculate method of ArthurMandelCalculator and found that I took the pixel coordinates to this method instead of the coordinates in the complex space. So I changed it in both ArthurFractalCalculator and ArthurMandelCalculator to use a complex number for calculate input. Cool.

Then I added two complex numbers to ArthurFractal which set the complex space of the fractal: cornerA and cornerB. I set the initial values in the constructor so that is good for the Mandelbrot set which is the default fractal. I also had to modify the calculate method of ArthurFractal to do the mapping between the complex space and pixel coordinates.

Finally I implemented the Mandelbrot set in the calculate method of ArthurMandelCalculator. It is fairly simple:

unsigned int ArthurMandelCalculator::calculate(ComplexNumber c)
{
unsigned int d = 0;

ComplexNumber z = c;

while ( d < ARTHUR_FRACTAL_CALCULATOR_MAX_ITERATIONS)
{

d++;
z = c + (z * z);

if(z.absPow2() > 4) return d;
}

return 0;
}

Then came the usual stuff. Modifying the project file, compilation etc.

Here is a screenshot of the final proggee:

You can download the sources here: fractal006

Posted in Arthur Fractal Generator, QT Programming.

Tagged with , , , , , , .