gpt4 book ai didi

C Mandelbrot 集着色

转载 作者:太空狗 更新时间:2023-10-29 17:24:57 24 4
gpt4 key购买 nike

我正在使用 C 编写以下代码。到目前为止,它一直在工作,并且已缩放到正确的级别等,但是我正在努力让颜色按我想要的方式工作。理想情况下,无论颜色如何,我都希望得到这样的结果:

Mandelbrot Set w/ correct colors

但是我的程序如下所示,目前会产生如下内容:

Current Mandelbrot Set

因此,如果我能得到任何帮助,让颜色变成我想要的样子,我将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define ITERMAX 100.0
#define DIVERGING 1.1
#define XMAX 500
#define YMAX 500
#define COLORINTENSITY 255

/* allow up to ITERMAX feedbacks searching for convergence
for the feedback
z0 = 0 + 0i
znew = z^2 + c
If we have not diverged to distance DIVERGING before ITERMAX feedbacks
we will assume the feedback is convergent at this value of c.
We will report divergence if |z|^2 > DIVERGING
*/

/* We will print color values for each pixel from (0, 0) to (XMAX, YMAX)
The color of pixel (cx, cy) will be set by convergent()
or by divergent()
depending on the convergence or divergence of the feedback
when c = cx + icy
*/

/* The max value of the red, green, or blue component of a color */

void convergent(); /* one color for convergence */
void divergent(); /* a different color for divergence */

void feedback(double *x, double *y, double cx, double cy);
void pixel(char red, char green, char blue);
FILE *fp;


int main()
{
fp = fopen("mandelbrot.ppm", "wb");
double x, y, cx, cy;
int iteration,squarex, squarey, pixelx, pixely;
double grow=1.0;

/* header for PPM output */
fprintf(fp, "P6\n# CREATOR: EK, BB, RPJ via the mandel program\n");
fprintf(fp, "%d %d\n%d\n",XMAX, YMAX, COLORINTENSITY);

for (pixely = 0; pixely < YMAX; pixely++) {
for (pixelx = 0; pixelx < XMAX; pixelx++) {
cx = (((float)pixelx)/((float)XMAX)-0.5)/grow*3.0-0.7;
cy = (((float)pixely)/((float)YMAX)-0.5)/grow*3.0;
x = 0.0; y = 0.0;
for (iteration=1;iteration<ITERMAX;iteration++) {
feedback(&x, &y, cx, cy);
if (x*x + y*y > 100.0) iteration = 1000;
}
if (iteration==ITERMAX) {
iteration = x*x + y*y;
pixel((char) 0, (char) 0, (char) 0);
}
else {
iteration = sqrt(x*x + y*y);
pixel((char) iteration, (char) 0, (char) iteration);
}
}
}
}

void feedback(double *x, double *y, double cx, double cy) {
/* Update x and y according to the feedback equation
xnew = x^2 - y^2 + cx
ynew = 2xy + cy
(these are the real and imaginary parts of the complex equation:
znew = z^2 + c)
*/
double xnew = (*x) * (*x) - (*y) * (*y) + cx;
double ynew = 2 * *x * *y + cy;
*x = xnew;
*y = ynew;
}

void pixel(char red, char green, char blue) {
/* put a r-g-b triple to the standard out */
fputc(red, fp);
fputc(green, fp);
fputc(blue, fp);
}

最佳答案

要修复 strip ,您需要遍历表格以找到迭代计数的最大值,然后缩放其他值以相对于此最大值(即。规范化 值) .您可能还希望以对数方式重新调整值以调整颜色变化的斜率

而且您可能不想直接在 RGB 空间中工作。如果您在 HSB 空间中定义颜色,则可以设置恒定的色调和饱和度,并根据归一化迭代次数按比例改变亮度。

关于C Mandelbrot 集着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36991937/

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