在我现有的Mandelbrot生成器中实现平滑的颜色算法

时间:2014-05-11 06:18:59

标签: algorithm mandelbrot

我目前正在编写一个Mandelbrot生成器,偶然发现了一个平滑的颜色算法,正如其名称所暗示的那样,创建了一个光滑的颜色"与我目前的例子相反。

enter image description here

如您所见,边缘情况非常明显且不平滑。

这是我的drawFractal()方法:

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; 
            }
        }
    }
}

根据平滑颜色伪代码,它要求:

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

话虽如此,从我的方法来看,我所拥有的最好的是这一行中的一个小小的RGB:

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

所以我不知道该怎么做。任何帮助都将非常感激。

附加也是获取迭代值的方法:

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;
        }
    }

正如你所知,有一个类可以返回复数,但这本身应该是自我解释的。

1 个答案:

答案 0 :(得分:1)

您的getIterValue需要返回包含最终值Z的对象以及迭代次数n。然后,您的伪代码将转换为

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

您可以使用

将其转换为0到1之间的值
nsmooth / maxIterations

您可以使用它以与您已经完成相同的方式选择颜色。

编辑:我看了一些伪代码以获得流畅的着色,我认为第一个日志应该是基础2:

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