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

C++ Mandelbrot program won't produce correct output

我正在尝试制作一个程序,通过制作 .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;
}

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