在遵循了这个非常简单的教程之后,该教程为您提供了所有代码(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();
            }
        }
    }



}


我不明白我在那做什么的意思,我尝试了一些事情,包括代码中的内容,但是没有运气。

最佳答案

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

关于c++ - 如何给曼德布罗上色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50113209/

10-11 18:40