【问题标题】:Implementing a smooth color algorithm into my existing Mandelbrot generator在我现有的 Mandelbrot 生成器中实现平滑的颜色算法
【发布时间】:2014-05-12 03:56:45
【问题描述】:

我目前正在编写一个 Mandelbrot 生成器,并偶然发现了一种平滑颜色算法,顾名思义,它创建了一种“平滑颜色”,而不是我目前的示例。

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

这是我的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;
        }
    }

正如你所知道的,有一个类可以返回复数,但这本身应该是不言自明的。

【问题讨论】:

    标签: algorithm mandelbrot


    【解决方案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)
    

    【讨论】:

    • 您好 - 非常感谢您的帮助。我得到了关于Z 的部分。至于n,也就是迭代次数,你的意思是最终迭代,最大迭代,还是我遗漏的其他东西?我还有一个getIterValue,其中包含您所说的许多内容。
    • 好的,我已经解决了Z Complex 问题。我添加了一个.getImag() 方法。这是我的最终方法:double nsmooth = iter + 1 - Math.log(Math.log(Math.abs(_Z.getMag()))/Math.log(2))/Math.log(2); 然而,这会打印出一个黑色方块,这是可以理解的,因为nsmooth 的值始终是一位十进制值。
    • @theGreenCabbage:您需要将其视为颜色范围的一部分,就像对 iter/maxIterations 所做的那样。
    • 我做了一些谷歌搜索,例如“bit-fiddled RGB”,但我不完全确定 RGB 组合值的颜色范围是多少。是 255 * 255 * 255 吗?
    • @theGreenCabbage:从maxColors的定义来看是R + 256*G + 65536*B
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2013-12-24
    • 2022-07-13
    • 2014-02-28
    相关资源
    最近更新 更多