Я пытаюсь создать программу, которая генерирует изображение стандартного набора Мандельброта, создавая файл .PPM. Программа не создает действительный файл PPM, и я не знаю, почему.C++ Программа Mandelbrot не даст корректного вывода
Вот мой код:
#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;
}
Приведите пример входного файла. – ooga
Не думайте, что ваш результат вполне соответствует спецификации PPM, см. Http://netpbm.sourceforge.net/doc/ppm.html – paisanco
Что говорит вам, что это не действительный PPM-файл? –