【问题标题】:c++ mandelbrot function exporting fully black imagec ++ mandelbrot函数导出全黑图像
【发布时间】: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


【解决方案1】:

在input 中,您正在创建临时对象并将数据读入临时对象。这不会改变main 中的对象。

您需要将c1 和c2 从main 传递给input。

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

在main 中,您有两个名称相似的对象,并为实部和虚部分配了1 的值。我不清楚main 中的c1/c2 和input 中的c1/c2 之间的关系是什么。但是,您需要使用Complex 类型的两个对象来调用input。它们是c1 和c2,还是其他尚未创建的对象,我不知道。

【讨论】:

  • 谢谢。我将它添加到 main 以尝试解决此问题,但它仍然是空白的,还有什么建议吗?
  • 现在,您必须在 main 方法中使用 input(c1,c2);。你试过了吗?
  • 它仍然是一种纯色,但现在它全部显示为所有颜色值为 225。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
相关资源
最近更新 更多