【发布时间】:2015-10-27 12:26:04
【问题描述】:
我应该编写一个根据文件导出 Mandelbrot 图像的程序,但由于某种原因,ppm 总是显示为纯黑色。我知道这意味着这些值总是被打印为 0,但我不知道为什么。 我正在使用的示例文件只是 -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();
}
【问题讨论】:
-
您可以尝试自己制作一个迭代的文本文件,然后用眼睛观察它以查看该部分是否正确发生。
标签: c++ mandelbrot