1

I recently found out about the Mandelbrot set and now I am trying to generate a Mandelbrot set in Processing 3.0. I found a tutorial on youtube about programming it and tried to impelement it in Progressing.

  background(255);
  size(1280, 720);
}

void draw(){
    int maxIteration = 100;
  
    double reMid = -0.75;
    double iMid = 0;
    
    double rangeR = 3.5;
    double rangeI = 2;
    
    double xPixels = 1280;
    double yPixels = 720;
    
    for(int x = 0; x < xPixels; x++){
        for(int y = 0; y < yPixels; y++){
            double xP       = (double)x / xPixels;
            double yP       = (double)y / yPixels;
          
            double cReal    = xP * rangeR + reMid - rangeR / 2;
            double cIm      = yP * rangeI + iMid - rangeI  / 2;
          
            double zReal    = 0;
            double zIm      = 0;
          
            int iteration   = 0;
            while(iteration < maxIteration && zReal * zReal + zIm * zIm <= 4) {
                double temp = zReal * zReal - cIm * cIm + cReal;
                zIm         = 2 * zReal * zIm + cIm;
                zReal       = temp;
                
                iteration++;
            }
            
            if(iteration >= maxIteration - 1){
                stroke(0);
            }
            else{
                stroke(255);
            }

            point(x, y);
        }
    }

}

But when i generated the Mandelbrot set, it looked different than it should:

img

I have already compared my code with the one in the video, but i did not find a mistake in my code. Does anyone know, what I did wrong?

Spektre
  • 49,595
  • 11
  • 110
  • 380
Niklas
  • 29
  • 1
  • 6
  • see [Can't find a way to color the Mandelbrot-set the way i'm aiming for](https://stackoverflow.com/a/56197067/2521214) for some inspiration – Spektre Aug 05 '20 at 06:02

1 Answers1

2

I zoomed it out a bit, and the long tails keep extending to infinity. Since all |C| > 2 should diverge, this makes it easy to find a specific case that fails, such as cReal = 2; cImg = -1.5;

Your code says it converges, but doing it by hand shows it diverges very quickly:

Z0 = 0 + 0i
Z1 = (0 + 0i)^2 + 2 - 1.5i = 2 - 1.5i
Z2 = 2*2 - 2*2*1.5i - 1.5^2 = 1.75 - 6i

Stepping through your code gives zReal, zImg

-1.5, -0.25
-0.75, -0.1875
-1.21875, -0.21484375
-0.976318359375, -0.2038421630859375
-1.1019703075289726, -0.20844837254844606
[...]

In other words, your loop is wrong. The immediately suspect line of code is this:

double temp = zReal * zReal - cIm * cIm + cReal;

It's doing cIm*cIm, but there's not supposed to be any multiplication of any components of C: it's simply added at the end.

So what's happened is that you accidentally switched zIm for cIm.

Switch them back and you should get a better result:

double temp = zReal * zReal - zIm * zIm + cReal;
that other guy
  • 116,971
  • 11
  • 170
  • 194