如何为Mandelbrot上色?

时间:2018-05-01 08:05:00

标签: c++ sfml

按照这个非常简单的教程提供了所有代码(http://warp.povusers.org/Mandelbrot/)后,我没有任何运气,想弄清楚如何在图片中为轮廓着色。

  
    

如果在我们的例子中我们将n映射到一个颜色,从0到MaxIterations / 2-1,颜色从黑色变为红色,从MaxIterations / 2到MaxIterations-1,颜色从红色变为白色,我们得到以下图片:

  
void compute_mandelbrot(double left, double right, double top, double bottom, double start, double end) //improved mandelbort following a tutorial
{
    double ImageHeight = 960;
    double ImageWidth = 960;

    //double MinRe = -2.5;
    //double MaxRe = 1.25;
    //double MinIm = -1.2;
    double MinRe = left;
    double MaxRe = right;
    double MinIm = top;
    double MaxIm = MinIm + (MaxRe - MinRe)*ImageHeight / ImageWidth;
    double Re_factor = (MaxRe - MinRe) / (ImageWidth - 1);
    double Im_factor = (MaxIm - MinIm) / (ImageHeight - 1);
    //unsigned MAX_ITERATIONS = 100;

    /*int r = 0x00, g = 0x00, b = 0x00;*/


    for (unsigned y = 0; y<ImageHeight; ++y)
    {
        double c_im = MaxIm - y*Im_factor;
        for (unsigned x = 0; x<ImageWidth; ++x)
        {
            //vertex.color = sf::Color(255, 255, 255, 255);
            //r = g = b = 0;

            double c_re = MinRe + x*Re_factor;

            double Z_re = c_re, Z_im = c_im;
            bool isInside = true;
            for (unsigned n = 0; n<MAX_ITERATIONS; ++n)
            {
                //vertex.color = sf::Color(0, 0, 0, 255); //figure is black
                //vertex.color = sf::Color(255, 255, 255, 255); //figure is white
                r = g = b = 0;

                double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
                if (Z_re2 + Z_im2 > 4)
                {

                    isInside = false;

                    auto half = MAX_ITERATIONS / 2.0;

                    if (n < half) {
                        r = n / half * 255;
                    }
                    else {
                        r = 255;
                        g = b = (n - half) / half * 255;
                    }

                    vertex.color = sf::Color(r, g, b, 255);

                    break;
                }

                Z_im = 2 * Z_re*Z_im + c_im;
                Z_re = Z_re2 - Z_im2 + c_re;


            }

            if (isInside)
            {
                //vertex.color = sf::Color(r, g, b, 255); //line pattern

                mutex.lock();
                vertex.position = sf::Vector2f(x, y);
                varray.append(vertex);
                mutex.unlock();
            } 
        }
    }



}

我不认为我理解我在那里做的事情,我尝试了一些事情,包括代码中的内容,但没有运气。

1 个答案:

答案 0 :(得分:1)

在第isInside = false;行,您的值为n,该点会使该值转义。使用此n值可根据颜色首选项设置点的r,g,b值。如果该点没有逃脱,它将是黑色的(或者你初始化r,g和b的任何东西)。

  

将n映射到一种颜色,从0到MaxIterations / 2-1,颜色从黑色变为红色,从MaxIterations / 2到MaxIterations-1,颜色从红色变为白色

这是简单的RGB数学,例如

auto half = MaxIterations/2.0;

if( n < half ) {
  r = n / half;
}else {
  r = 1.0;
  g = b = (n - half) / half;
}

,如果你使用整数作为颜色,则rgb在0-1范围内,乘以255