0
$\begingroup$

I'm writing an algorithm to generate the Mandelbrot set in Java. However, the final picture is incorrect. It looks like this

enter image description here

I was wondering if the algorithm was incorrect.

public void Mandlebrot() {
    float reMax=2;
    float imMax=2;
    float reMin=-2;
    float imMin=-2;
    float xDelta = (reMax - reMin)/test.width;
    float yDelta = (imMax - imMin)/test.height;

    int N=20000;
    float complex = imMin;
    for(int y=0; y<test.height; y++) {
        float real = reMin;
        for(int x=0; x<test.width; x++) {
            int count = 0;
            float complexC = 0.4f;
            float realC = 0.3f;

            while(count<N && complexC*complexC+realC*realC<=4.0f) {
                complexC = realC*realC-complexC*complexC + complex;
                realC = 2*complexC*realC + real;
                count++;
            }

            if(complexC*complexC+realC*realC<=4.0f) {
                setPixel(x,y) = 1000;
            } else {
                setPixel(x,y) = 1+ 1000*count/N;
            }

            real+=xDelta;
        }
        complex+=yDelta;
    }
}
$\endgroup$
  • 1
    $\begingroup$ A description of your algorithm in psuedocode would be more appropriate for this site. $\endgroup$ – Austin Mohr Jul 6 '13 at 12:21
  • $\begingroup$ It is called Mandelbrot, by the way. See en.wikipedia.org/wiki/Mandelbrot_set $\endgroup$ – martin.koeberl Jul 6 '13 at 12:22
  • 1
    $\begingroup$ I think you knowingly/unknowingly created java-applet tag. (it has zero tags, correct me if not). I think it is not appropriate for this site. $\endgroup$ – Dinesh Jul 6 '13 at 12:26
  • $\begingroup$ @Edit: The java-applet has six questions tagged (without counting this one). $\endgroup$ – Asaf Karagila Jul 6 '13 at 12:28
  • 1
    $\begingroup$ Why do you initialise complexC and realC to $0.4$ and $0.3$ respectively? They should both be initialised to $0$. $\endgroup$ – Peter Taylor Jul 6 '13 at 13:20
4
$\begingroup$

In the loop

while(count<N && complexC*complexC+realC*realC<=4.0f) {
    complexC = realC*realC-complexC*complexC + complex;
    realC = 2*complexC*realC + real;
    count++;
}

You use the updated complexC to compute the new realC, but you ought to use the old one:

float oldC = complexC;
complexC = realC*realC-complexC*complexC + complex;
realC = 2*oldC*realC + real;
count++;

Besides, you seem to have flipped the real and imaginary parts. I think that only rotates the picture, though.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.