Skip to content


A QT Project – Fractal Generator – Preparing for Fractals

I continued developing the Arthur Fractal Generator. I made some changes to existing code and also added new code. The main target is to facilitate the fractal calculation and rendering. From now on I will not copy-paste whole files to the post, because they are getting too long. Download the source archive from the end of the post and you can follow there what I’m writing about.

First of all I removed the pixmap from the ArthurCanvas widget in Designer. Simply by editing its properties. I will add the pixmap in the code. Check ui_main.h or open the ui file in Designer.

Then I added a new private property to ArthurCanvas: ArthurFractal *fractal; It belongs to a new class, that represents the fractal. Check arthur_canvas.h. I decided to separate the data representation from the GUI as much as possible. A fractal is usually drawn by using an indexed color palette. Therefor I cannot use QPixmap for the data representation itself. It will be used only for rendering the results. So the Fractal will have a palette, a two dimensional array for storing the color indexes for each pixels and a calculator which will calculate the color index for a certain pixel.

I also added initialization code to the ArthurCanvas constructor in arthur_canvas.cpp

fractal = new ArthurFractal(this);
fractal->setSize(ARTHUR_CANVAS_DEFAULT_WIDTH,
ARTHUR_CANVAS_DEFAULT_HEIGHT);
fractal->calculate();

QPixmap pm(ARTHUR_CANVAS_DEFAULT_WIDTH, ARTHUR_CANVAS_DEFAULT_HEIGHT);

fractal->copyToPixmap(&pm);

setPixmap(pm);

Here I create a new ArthurFractal. Then I set the size, which will create and initialize the two dimensional array for storing the color indexes. After this I call calculate, which calculates the fractal. Then I create a new pixmap with the same size as the fractal. Finally I convert the indexed data representation to the pixmap and show it. Probably you are wondering what fractal the calculate method is actually calculating. I will discuss this later. Now it is enough to know there is a default calculator added when I construct the ArthurFractal.

So far so good, so lets look at the arthur_fractal.h header file. As you can see the palette is a QList holding QColor data. The two dimensional array called map is a QList of QLists which are holding unsigned integers, the indexes. Then it has the width and height private attributes to hold the size of the fractal, and it also has a public method called setSize which can change these values. Changing the size will reinitialize the map.

As I mentioned above the fractal also has a calculator. In this case it is a pointer to a new data type ArthurFractalCalculator. ArthurFractalCalculator has a method calculate which takes the coordinates of the pixel in question and returns the color index value. ArthurFractal has also a calculate method, which calls the calculate method of ArthurFractalCalculator for each pixels.

The copyToPixmap method simply goes through all the pixels in the map and gets the RGB value from the palette by the color index. Then sets the corresponding pixel in the pixmap.

Check the implementations is arthur_fractal.cpp

The fractals are basically distinguished by the calculator. Therefor I separated the calculator to a new class. This will allow to change the calculator on the fly and it will also be easier to develop new calculators. At the moment the calculator only have a calculate method which is virtual and has no implementation. We will inherit the actual calculators from this class. Check arthur_fractal_calculator.h and arthur_fractal_calculator.cpp for the calculator abstract class.

If you checked arthur_fractal.cpp you could find that in the constructor I initialize the calculator to ArthurMandelCalculator which will be the default calculator. As you probably guessed ArthurMandelCalculator is inherited from ArthurFractal calculator. From the name it follows it will calculate a Mandelbrot set some day :) At the moment it simply returns 0 for each coordinates which means the first color from the platette. It will result a black image with the default palette. Check arthur_mandel_calculator.h and arthur_mandel_calculator.cpp

Also check the new project file. I simply added the new sources there.

Here is a screenshot:

You can download the sources from here: fractal005

Posted in Arthur Fractal Generator, QT Programming.

Tagged with , , , , , , , .