0

I'm supposed to be writing a program that exports a Mandelbrot image according to a file, but for some reason the ppm always comes out solid black. I understand that this means the values are always being printed as 0, but I'm not sure why. The example file I'm using is just -1 1 .5 -.5

#include <iostream>
#include <fstream>
#include <complex>
#include <cmath>
using namespace std;

const int MAXITERS = 225;

struct Complex {
    double real;
    double imag;
};

void input() {
    Complex c1;
    c1.real = 0;
    c1.imag = 0;
    Complex c2;
    c2.real = 0;
    c2.imag = 0;
    ifstream fin;
    fin.open("mandelinput.txt");
    fin >> c1.real >> c1.imag >> c2.real >> c2.imag;
}


double loc1(double i, Complex c1, Complex c2) {
    double r1 = c1.real - c2.real;
    return i * (r1 / 512) + c1.real;
}

double loc2(double j, Complex c1, Complex c2) {
    double r2 = c1.imag - c2.imag;
    return j * (r2 / 512) + c1.imag;
}
int mandelbrot(double x,double y) {
    Complex z;
    int i = 0;
    z.real = 0.0;
    z.imag = 0.0;

    while (i<MAXITERS && z.real * z.real + z.imag * z.imag < 4) {
        double zt = z.real * z.real - z.imag * z.imag + x;
        z.imag = 2 * z.real * z.imag + y;
        z.real = zt;
        ++i;
    }

    return i;
}

int main() {
    ofstream fout;
    int i,j,m;
    double x = 0;
    double y = 0;
    Complex c1;
    c1.real = 1;
    c1.imag = 1;
    Complex c2;
    c2.real = 1;
    c2.imag = 1;
    input();
    Complex z;
    z.real = 1;
    z.imag = 1;
    fout.open("mandeloutput.ppm");
    fout << "P3\n";
    fout << "512" << " " << "512" << endl;
    fout << "255\n";
    for (j = 0; j < 512; j++) {

        for (i = 0; i < 512; i++) {         
            x = loc1(i,c1,c2);
            y = loc2(j,c1,c2);
            m = mandelbrot(x,y);
            int r = (m % 255);
            int g = (m % 255);
            int b = (m % 255);
            fout << r << " " << g << " " << b << " ";
            }
        fout << endl;
    }
    fout.close();

}
  • You could try making a textfile of the iterations themselves, then eyeball it to see if that part is happening correctly. – Weather Vane Oct 27 '15 at 18:18

1 Answers1

0

In input, you are creating temporary objects and reading the data into the temporary objects. That does not change the objects in main.

You need to pass c1 and c2 to input from main.

void input(Complex& c1, Complex& c2) {
    c1.real = 0;
    c1.imag = 0;
    c2.real = 0;
    c2.imag = 0;
    ifstream fin;
    fin.open("mandelinput.txt");
    fin >> c1.real >> c1.imag >> c2.real >> c2.imag;
}

In main, you have two objects that are similarly named and assigned value of 1 to both the real part and the imaginary part. It's not clear to me what the relationships between c1/c2 in main and c1/c2 in input are. However, you'll need to call input using two objects of type Complex. Whether they are c1 and c2, or some other yet to be created object, I don't know.

R Sahu
  • 204,454
  • 14
  • 159
  • 270