Question

I am currently writing a Mandelbrot generator, and stumbled onto a smooth color algorithm that creates a, as its name suggests, a "smooth color" as opposed to the example I currently have.

enter image description here

As you can see, the edge cases are very evident and non-smooth.

Here is my drawFractal() method:

public static void drawFractal()
{
    Complex Z;
    Complex C;

    double x;
    double y;

    // The min and max values should be between -2 and +2
    double minX = -2.0; // use -2 for the full-range fractal image
    double minY = -2.0; // use -2 for the full-range fractal image
    double maxX = 2.0; // use 2 for the full-range fractal image
    double maxY = 2.0; // use 2 for the full-range fractal image

    double xStepSize = ( maxX - minX ) / width;
    double yStepSize = ( maxY - minY ) / height;
    int maxIterations = 100;
    int maxColors = 0xFF0000;

    // for each pixel on the screen
    for( x = minX; x < maxX; x = x + xStepSize)
    {
        for ( y = minY; y < maxY; y = y + yStepSize )
        {
            C = new Complex( x, y );
            Z = new Complex( 0, 0 );
            int iter = getIterValue( Z, C, 0, maxIterations );

            int myX = (int) ( ( x - minX ) / xStepSize );
            int myY = (int) ( ( y - minY ) / yStepSize );
            if ( iter < maxIterations )
            {
                myPixel[ myY * width + myX ] = iter * ( maxColors / maxIterations ) / 50; 
            }
        }
    }
}

According to smooth color pseudo-code, it calls for this:

nsmooth := n + 1 - Math.log(Math.log(zn.abs()))/Math.log(2)

With that said, from my method, the best I have is a bit-fiddled RGB from this line:

if ( iter < maxIterations )
{
    myPixel[ myY * width + myX ] = iter * ( maxColors / maxIterations ) / 50; 
}

So I am at loss as to what to do. Any help would be very appreciated.

Attached is also the method to get my iteration value:

public static int getIterValue( Complex Z, Complex C, int iter, int maxNumIters )
    {
        if ( Z.getMag() < 2 && iter < maxNumIters )
        {
            Z = ( Z.multiplyNum( Z )).addNum( C );
            iter++;
            return getIterValue( Z, C, iter, maxNumIters );
        }
        else
        {
            return iter;
        }
    }

As you can tell there's a class to return Complex numbers but that should be self explanatory in itself.

Was it helpful?

Solution

Your getIterValue needs to return an object containing the final value of Z as well as the number of iterations n. Your pseudo-code would then translate to

nsmooth := iter.n + 1 - Math.log(Math.log(iter.Z.abs())/Math.log(2))

You can translate this to a value between 0 and 1 with

nsmooth / maxIterations

with which you can pick a colour in much the same way that you are doing already.

Edit: I took a look at some psuedo-code for smooth colouring and I think that the first log should be base 2:

nsmooth := iter.n + 1 - Math.log(Math.log(iter.Z.abs())/Math.log(2))/Math.log(2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top