Mandelbrot-Menge zeichnen Prog

Hey Leute, so kommen meine letzen Threads hier zusammen.

Ich habe mich an einem netten kleinen Programm versucht um die Mandelbrot-Menge darzustellen.
Das Problem ist, meiner Meinung nach ist es der Richtige Algorithmus den ich verwende und auch in meinen Klassen usw. finde ich keine Fehler, aber dennoch stimmt mein Bild nicht mit der Mandelbrot-Menge oder dem Apfelmännchen überein.

Ich wollte jetzt einfach mal den entscheidenden Code hier posten, und euch um Hilfe bitten, euch das mal durchzuschauen und mir zu sagen ob euch etwas auffällt.

Code:
commplex_n.h:

#include <iostream>
#include <math.h>

using namespace std;


class complex_n
{
    public:
        void set_x_y(double a, double b);
        void out();
        complex_n operator^(double n);
        complex_n operator+(complex_n a);
        complex_n operator=(complex_n c);
        int mandelbrot(complex_n z0);
        double abs(complex_n a);

    private:
        double x, y;
};


complex_n.cpp:

#include "complex_n.h"

complex_n complex_n::operator^(double n)
{
    x = x*x - y*y;
    y = 2*x*y;

    return *this;

}

complex_n complex_n::operator=(complex_n c)
{
    x = c.x;
    y = c.y;

    return *this;
}

void complex_n::set_x_y(double a, double b)
{
    x = a;
    y = b;
}

complex_n complex_n::operator+(complex_n a)
{
    x+=a.x;
    y+=a.y;

    return *this;
}

void complex_n::out()
{
    cout<<"("<<x<<";"<<y<<")"<<endl;
}

double complex_n::abs(complex_n a)
{
    complex_n z;
    double erg;

    z = a;
    erg = z.x*z.x - z.y*z.y;
    if(erg < 0)
    {
        erg*=-1;
    }
    return erg;
}


int complex_n::mandelbrot(complex_n c)
{
    int n = 1;
    complex_n z[180];
    z[0] = c;

    while(n <= 180)
    {
        z[n] = (z[n-1]^2) + c;

        //z[n].out(); cout<<" n: "<<n;

        if(abs(z[n]) > 4)
        {
            break;
        }
        ++n;
    }

    return n;
}

Code:
main.cpp:

#include <allegro.h>
#include <iostream>
#include "complex_n.h"
using namespace std;


int main()
{
    int x_size = 1000;
    int y_size = 1000;

    int x;
    int y;

    complex_n c1, c2;


    allegro_init();
    install_keyboard();
    install_mouse();
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, x_size, y_size, 0, 0);

    show_os_cursor(MOUSE_CURSOR_ARROW);

    clear_to_color( screen, makecol( 255, 255, 255));

    acquire_screen();

    int n;


    for(double a = -5; a < 5; a+=0.01)
    {
        for(double b = -5; b < 5; b+=0.01)
        {

            x = a*100 + 500;
            y = b*100 + 500;

            c1.set_x_y(a,b);

            n = c2.mandelbrot(c1);  //aufruf der Mandelbrot-Funktion

            if(n >= 181)
            {
                putpixel( screen, x, y, makecol( 0, 0, 0));
            }

            if(n <= 1)
            {
                putpixel( screen, x, y, makecol( 255, 255, 255));
            }
            if(n < 4 && n > 1)
            {
                putpixel( screen, x, y, makecol( 0, 255, 0));
            }

            if(n < 8 && n >= 4)
            {
                putpixel( screen, x, y, makecol( 255, 0, 0));
            }

            if(n < 180 && n >= 8)
            {
                putpixel( screen, x, y, makecol( 0, 0, 255));
            }
        }
    }

    for(int b = 0; b<y_size; ++b)
    {
        putpixel( screen, 200, b, makecol( 0, 0, 0));
        putpixel( screen, 300, b, makecol( 0, 0, 0));
        putpixel( screen, 400, b, makecol( 0, 0, 0));
        putpixel( screen, 500, b, makecol( 0, 0, 0));
        putpixel( screen, 600, b, makecol( 0, 0, 0));
        putpixel( screen, 700, b, makecol( 0, 0, 0));
        putpixel( screen, 800, b, makecol( 0, 0, 0));
    }
    textout(screen, font, "0", 500, 4, 255);


    for(int a = 0; a<x_size; ++a)
    {
        putpixel( screen, a, 200, makecol( 0, 0, 0));
        putpixel( screen, a, 300, makecol( 0, 0, 0));
        putpixel( screen, a, 400, makecol( 0, 0, 0));
        putpixel( screen, a, 500, makecol( 0, 0, 0));
        putpixel( screen, a, 600, makecol( 0, 0, 0));
        putpixel( screen, a, 700, makecol( 0, 0, 0));
        putpixel( screen, a, 800, makecol( 0, 0, 0));
    }

    release_screen();

    readkey();

    return 0;
}

ich weiß es ist viel Code zum durchschauen und ich hab jetzt so spät auch nicht mehr die Zeit gefunden ihn auszukommentieren, aber ich glaube er ist eigentlich ganz gut verständlich und erklärt sich aus Klassen/Var-Namen.
Falls man ihn doch nicht peilt weil er zu unüberischtlich ist, poste ich morgen einen mit Comments nach.

Hoffe es kann mir jemand helfen ich habe nämlich wirklich keine Idee mehr woran es liegen kann.
Hier noch das Bild das es erstellt -> Anhang: (Die Farbverteilung ist auf dem Bild etwas anders als sie dem Code zu entnehmen ist)
 
Das hier sieht verdächtig aus.
Code:
complex_n complex_n::operator^(double n)
{
    x = x*x - y*y;
    y = 2*x*y;

    return *this;

}

Bei der Zuweisung von y benutzt den neuberechneten x Wert und den alten y Wert. Hier muss wohl mit temporären Variablen gearbeitet werden.

Code:
    double xt = x*x - y*y;
    double yt = 2*x*y;

    x = xt;
    y = yt;
Und warum überladest du den Operator ^ mit dem Parameter "n", wenn du diesen an der Stelle gar nicht verwendest?
 
zu Punkt 1, das ist mir noch gar nicht aufgefallen aber das klingt echt logisch, hammer danke werds gleich mal schnell ausprobieren.
zu Punkt 2: Die Methode wie sie jetzt steht ist noch ne vereinfachung ich will noch etwas einbauen das ich beliebig hoch nehmen kann.

DANKE !!! ganz ehrlich ... da wäre ich wahrscheinlich nicht mehr drauf gekommen, das sind immer diese dummen Fehler eig stimmt alles im groben aber es ist halt doch etwas falsch ^^

CLOSE
 
Zuletzt bearbeitet:
Zurück
Oben