gpt4 book ai didi

C++ Mandelbrot 程序不会产生正确的输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:34 24 4
gpt4 key购买 nike

我正在尝试制作一个程序,通过制作一个 .PPM 文件来生成标准 Mandelbrot 集的图像。该程序没有生成有效的 PPM 文件,我不知道为什么。

这是我的代码:

#include <fstream>
#include <iostream>

using namespace std;

/*
For each pixel (Px, Py) on the screen, do:
{
x0 = scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1))
y0 = scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1))
x = 0.0
y = 0.0
iteration = 0
max_iteration = 1000
while ( x*x + y*y < 2*2 AND iteration < max_iteration )
{
xtemp = x*x - y*y + x0
y = 2*x*y + y0
x = xtemp
iteration = iteration + 1
}
color = palette[iteration]
plot(Px, Py, color)
}
*/

int findMandelBrot(double cr, double ci, int max_iterations){
int i = 0;
double zr = 0.0, zi = 0.0;
while (i > max_iterations && zr * zr + zi * zi < 4.0){
double temp = zr * zr - zi * zi;
zi = 2.0 * zr * zi + ci;
zr = temp;
i++;
}
return i;
}

double mapToReal(int x, int imageWidth, double minR, double maxR){
double range = maxR - minR;

return x * (range / imageWidth) + minR;
}

double mapToImaginary(int y, int imageWidth, double minI, double maxI){
double range = maxI - minI;

return y * (range / imageWidth) + minI;

}

int main(){

ifstream fin;
fin.open ("input.txt");
int imageWidth, imageHeight, maxN;
double minR, maxR, minI, maxI;

if (!fin.is_open()){
cerr << "Couldn't load input.txt file" << endl;
return 0;
}

fin >> imageWidth >> imageHeight >> maxN;
fin >> minR >> maxR >> minI >> maxI;
fin.close();

ofstream fout("output_image.ppm");
fout << "P3" << endl;
fout << imageWidth << " " << imageHeight;
fout << "256" << endl;

for (int y = 0; y < imageHeight; y++){
for (int x = 0; x < imageWidth; x++){
double cr = mapToReal(x, imageWidth, minR, maxR);
double ci = mapToImaginary(y, imageHeight, minI, maxI);

int n = findMandelBrot(cr, ci, maxN);

int r = (n % 256);
int g = (n % 256);
int b = (n % 256);

fout << r << " " << g << " " << b << " ";
}
fout.close();
}
fout.close();
cout << "Finished! " << endl;

cin.ignore();
cin.get();
return 0;
}

最佳答案

调试的良好开端是使用简单的输入运行程序(例如生成 8x5 输出图像),然后查看输出。由于 PPM 易于人类阅读,您将看到您只获得了 8 个样本。这应该是第一行没问题的线索,第二行和第二行之间有问题。现在放大到行循环,您会看到您已经在要发出换行符的地方编写了 fout.close()

接下来您会发现所有值都为零。这有点难以诊断,但如果您查看 findMandelBrot 并在脑海中逐一检查,您将进入带有 iwhile 循环> 等于 0,您应该发现循环永远不会进入。

我已经稍微修改了您的代码。除了错误修复,我还有

  • 使用 std::complex 而不是重新发明轮子 - 如果您不熟悉它,请在您最喜欢的引用资料中查找它
  • 假设一旦它开始工作你会对颜色做一些不同的事情,或者我会把输出简化为 PGM 而不是 PPM
  • 添加了对文件读写的最少检查
  • 包括对解释我的修复的代码的注释。

代码如下:

#include <fstream>
#include <iostream>
#include <complex>

using namespace std;


// Converted to take a std::complex to make the arithmetic clearer
int findMandelBrot(complex<double> c, int max_iterations)
{
int i = 0;
complex<double> z = 0;
// was while(i > max_iterations ...) which would make this always
// return false
while (i <= max_iterations && norm(z) < 4.0) {
z *= z;
z += c;
i++;
}
return i;
}

double mapToReal(int x, int imageWidth, double minR, double maxR)
{
double range = maxR - minR;
return x * (range / imageWidth) + minR;
}

double mapToImaginary(int y, int imageWidth, double minI, double maxI)
{
double range = maxI - minI;
return y * (range / imageWidth) + minI;

}

int main()
{
ifstream fin;
fin.open("input.txt");
int imageWidth, imageHeight, maxN;
double minR, maxR, minI, maxI;

if (!fin.is_open()) {
cerr << "Couldn't load input.txt file" << endl;
return EXIT_FAILURE;
}

fin >> imageWidth >> imageHeight >> maxN;
fin >> minR >> maxR >> minI >> maxI;

// Check whether we managed to read the values
if (!fin) {
cerr << "Failed to read input.txt file" << endl;
return EXIT_FAILURE;
}
fin.close();

ofstream fout("output_image.ppm");
if (!fout) {
// something went wrong
cerr << "Couldn't open output file" << endl;
return EXIT_FAILURE;
}
fout << "P3" << endl;
fout << imageWidth << " " << imageHeight;
fout << " " << "256" << endl;

for (int y = 0; y < imageHeight; y++) {
for (int x = 0; x < imageWidth; x++) {
double cr = mapToReal(x, imageWidth, minR, maxR);
double ci = mapToImaginary(y, imageHeight, minI, maxI);

int n = findMandelBrot({cr, ci}, maxN);

int r = (n % 256);
int g = (n % 256);
int b = (n % 256);

fout << r << " " << g << " " << b << " ";
}
// was fout.close() - ending the image after first line
fout << endl;

// Periodically check for errors
if (!fout) {
// something went wrong
cerr << "Write failed" << endl;
return EXIT_FAILURE;
}
}
fout.close();

return EXIT_SUCCESS;
}

这应该能让你继续更进一步。

关于C++ Mandelbrot 程序不会产生正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29906527/

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