maxun
maxun

Reputation: 25

C++ Mandelbrot program won't produce correct output

I am trying to make a program that generates an image of the standard Mandelbrot set by making a .PPM file. The program doesn't produce a valid PPM file and I have no clue why.

Here is my code:

#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;
}

Upvotes: 2

Views: 594

Answers (1)

Toby Speight
Toby Speight

Reputation: 30860

A good start with debugging is to run the program with simple inputs (e.g. to generate a 8x5 output image), then look at the output. Since PPM is easily human-readable, you'll see that you get only 8 samples. That should be a clue that the first row is okay, and there's a problem between that and the second row. Now zoom in to the row loop and you'll see that you've written fout.close() where you meant to emit a newline.

The next thing you'll spot is that all your values come out as zero. That's a bit harder to diagnose, but if you look in findMandelBrot and step through in your mind, you'll go into the while loop with i equal to 0, and you should spot that the loop never gets entered.

I've re-worked your code a little. In addition to the bug fixes, I have

  • used std::complex rather than re-invent the wheel - look it up in your favourite reference if you're not familiar with it
  • assumed you'll do something different with the colours once it's working, or I would have simplified the output to a PGM rather than PPM
  • added minimal checking of the reading and writing of files
  • included comments to the code explaining my fixes.

Here's the code:

#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;
}

That should allow you to continue a little further.

Upvotes: 1

Related Questions