How to smooth this Java Mandelbrot algorithm?

540 views Asked by At

I have a function which generates the values of the Mandelbrot Set within the range of [0-255]. After calculating, I paint the result with the help of a pre-generated palette, that also has 256 values.

The calculation:

int MAX = 255;

private int calculateMandel(double positionX, double positionY) {
        int valueOne = 0;
        double complexReal = 0.0;
        double complexImaginery = 0.0;
        double complexRealSquare = 0.0;
        double complexImaginerySquare = 0.0;

        while (valueOne < MAX && complexRealSquare + complexImaginerySquare < 4.0) {
            complexImaginery = 2.0 * complexReal * complexImaginery + positionY;
            complexReal = complexRealSquare - complexImaginerySquare + positionX;
            complexRealSquare = complexReal * complexReal;
            complexImaginerySquare = complexImaginery * complexImaginery;

            valueOne++;
        }

        return valueOne;
    }

The result:

enter image description here

It is clearly a very bad rendering, because the levels can be seen, it is not smooth enough. How can I make my picture smoother? Like this: http://upload.wikimedia.org/wikipedia/commons/2/21/Mandel_zoom_00_mandelbrot_set.jpg

2

There are 2 answers

0
placeybordeaux On

The way that the mandelbrot set works is that it tests how many iterations it takes to converge to a finite number, you detect this when complexRealSquare + complexImaginerySquare < 4.0 The MAX value you have set to 255 limits the number of iterations it will take to converge and therefore has a sharp cut off giving you those lines you are seeing.

To eliminate the lines you need to have more than 255 colors to map to. You can do this by painting with not just blue, but for instance start with red and move from red to blue.

0
Michael S On

To reduce pixelation you could take an average calculation for 4 or 5 points {(positionX+-delta, positionY+-delta)} for a small value delta less than half the width of the pixel. This should smooth out the picture by giving you the "typical" color that an area under a pixel should look like from above. But it won't remove the dark blue outer bands because of the integer nature of the number of iterations you are computing. There are many ways to tailor the color scheme...I was about to suggest something similar to the histogram method, which is also covered in the article linked by @leonbloy in the comments. Don't go with my hack suggestion before reading that article, I think it's your answer.