11/**
22 * The Mandelbrot Set
33 * by Daniel Shiffman.
4- *
4+ * (slight modification by l8l)
5+ *
56 * Simple rendering of the Mandelbrot set.
67 */
78
@@ -43,21 +44,30 @@ for (int j = 0; j < height; j++) {
4344 float x = xmin;
4445 for (int i = 0 ; i < width ; i++ ) {
4546
46- // Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
47+ // Now we test, as we iterate z = z^2 + c does z tend towards infinity?
4748 float a = x;
4849 float b = y;
4950 int n = 0 ;
51+ double max = 4.0 ; // Infinty in our finite world is simple, let's just consider it 4
52+ double absOld = 0.0 ;
53+ float convergeNumber = maxiterations; // this will change if the while loop breaks due to non-convergence
5054 while (n < maxiterations) {
55+ // We suppose z = a+ib
5156 float aa = a * a;
5257 float bb = b * b;
53- float twoab = 2.0 * a * b;
54- a = aa - bb + x;
55- b = twoab + y;
56- // Infinty in our finite world is simple, let's just consider it 16
57- if (dist (aa, bb, 0 , 0 ) > 4.0 ) {
58+ double abs = Math . sqrt(aa+ bb);
59+ if (abs > max) { // |z| = sqrt(a^2+b^2)
60+ // Now measure how much we exceeded the maximum:
61+ float diffToLast = (float ) (abs - absOld);
62+ float diffToMax = (float ) (max - absOld);
63+ convergeNumber = n + diffToMax/ diffToLast;
5864 break ; // Bail
5965 }
66+ float twoab = 2.0 * a * b;
67+ a = aa - bb + x; // this operation corresponds to z -> z^2+c where z=a+ib c=(x,y)
68+ b = twoab + y;
6069 n++ ;
70+ absOld = abs;
6171 }
6272
6373 // We color each pixel based on how long it takes to get to infinity
@@ -66,11 +76,11 @@ for (int j = 0; j < height; j++) {
6676 pixels [i+ j* width ] = color (0 );
6777 } else {
6878 // Gosh, we could make fancy colors here if we wanted
69- float norm = map (n , 0 , maxiterations, 0 , 1 );
79+ float norm = map (convergeNumber , 0 , maxiterations, 0 , 1 );
7080 pixels [i+ j* width ] = color (map (sqrt (norm), 0 , 1 , 0 , 255 ));
7181 }
7282 x += dx;
7383 }
7484 y += dy;
7585}
76- updatePixels ();
86+ updatePixels ();
0 commit comments