C++曼德布洛特程序不会产生正确的输出

C++ Mandelbrot program won't produce correct output

本文关键字:输出 布洛特 程序 C++      更新时间:2023-10-16

我正试图制作一个程序,通过制作.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并在脑海中逐步了解,你会进入while循环,i等于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;
}

这应该会让你继续前进一步。