문제

Okay, here's a weird one that I'm having problems with (compiled with gcc btw)

Below is source for a Mandelbrot fractal generator for command prompt. I've done this before and I wanted to speed test myself to see how fast I could produce the code required to actually generate a Mandelbrot fractal in the command prompt. Every so often I do this to kinda test myself out for fun

Anyways I've run into a new problem and I can't quite figure out what the issue is. When the fractal renders no matter how many iterations or what escapeValue I set it will ALWAYS appear as an oval! Its NOT supposed to do that.

For all you mandelbrot/cpp geeks out there can you help me identify why I'm not getting more of a 'mandelbrot' shape?

#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!

도움이 되었습니까?

해결책

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.

다른 팁

Your escapedvalue is too low it should be 4.00f.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top