gpt4 book ai didi

c++ - 如何给 Mandelbrot 着色?

转载 作者:行者123 更新时间:2023-11-28 04:41:24 25 4
gpt4 key购买 nike

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

If in our example we map n to a color so that from 0 to MaxIterations/2-1 the color goes from black to red and from MaxIterations/2 to MaxIterations-1 the color goes from red to white, we get the following image:

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 的东西)。

map n to a color so that from 0 to MaxIterations/2-1 the color goes from black to red and from MaxIterations/2 to MaxIterations-1 the color goes from red to white

这是简单的 RGB 数学,例如

auto half = MaxIterations/2.0;

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

它给出 0-1 范围内的 rgb,如果您使用整数作为颜色则乘以 255

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com