2

好的,这是一个我遇到问题的奇怪问题(用 gcc btw 编译)

下面是用于命令提示符的 Mandelbrot 分形生成器的源代码。我以前做过这个,我想自己进行速度测试,看看我能多快生成在命令提示符下实际生成 Mandelbrot 分形所需的代码。每隔一段时间,我都会这样做以测试自己的乐趣

无论如何,我遇到了一个新问题,我无法弄清楚问题是什么。当分形渲染时,无论我设置多少次迭代或什么 escapeValue,它总是会显示为椭圆形!它不应该那样做。

对于所有 mandelbrot/cpp 极客,你能帮我确定为什么我没有得到更多的“mandelbrot”形状吗?

#include <stdio.h>
#include <math.h>

#define DOSWidth 80
#define DOSHeight 25

int iterations = 1024;
float escapeValue = 3.0f;

struct ivar {
    ivar(float _x, float _i) {
        x = _x;
        i = _i;
    }
    void log() {printf("(%g%c%gi)", x, (i<0)?'-':'+', fabs(i));}
    float magnitude() {return sqrtf(x*x+i*i);}
    ivar square() {return ivar(x, i)*ivar(x, i);}

    ivar operator + (ivar v) {return ivar(x+v.x, i+v.i);};
    ivar operator - (ivar v) {return ivar(x-v.x, i-v.i);};
    ivar operator * (ivar v) {return ivar(x*v.x-(i*v.i), x*v.i+i*v.x);};

    float x, i;
};

struct rect {
    rect(float _x, float _y, float _width, float _height) {
        x = _x;y = _y;width = _width;height = _height;
    }

    void setCenter(float cx, float cy) {
        x = cx-width/2.0f;
        y = cy-width/2.0f;
    }

    void log() {printf("(%f, %f, %f, %f)", x, y, width, height);}

    float x, y;
    float width, height;
};

int main() {
    rect region = rect(0, 0, 2.5f, 2.0f);
    region.setCenter(0, 0);
    float xSize = region.width / (float)DOSWidth;
    float ySize = region.height / (float)DOSHeight;
    for(int y=0;y<DOSHeight;y++) {
        for(int x=0;x<DOSWidth;x++) {
            ivar pos = ivar(x*xSize+region.x, y*ySize+region.y);
            bool escapes = false;
            for(int i=0;i<iterations;i++) {
                if(pos.magnitude() > escapeValue) {
                    escapes = true;
                    break;
                }
                pos = pos.square();
            }
            if(escapes)printf(" ");
            else printf("X");
        }
    }
}

Thanks if you got this far, appreciate your help!

4

2 回答 2

3

You're just recursively squaring pos until its magnitude exceeds the limit. That won't produce a fractal; it will produce a unit circle.

You need to add the (x,y) coordinates to the squared value after every iteration. See Wikipedia.

EDIT: A couple small changes and voila.

于 2012-12-25T10:04:36.417 回答
0

Your escapedvalue is too low it should be 4.00f.

于 2012-12-25T10:01:28.033 回答