Skip to content

Commit 53f7959

Browse files
authored
Merge pull request #765 from l8l/patch-1
Update Mandelbrot.pde
2 parents 070a6e0 + 1088d53 commit 53f7959

File tree

1 file changed

+19
-9
lines changed
  • content/examples/Topics/Fractals and L-Systems/Mandelbrot

1 file changed

+19
-9
lines changed

content/examples/Topics/Fractals and L-Systems/Mandelbrot/Mandelbrot.pde

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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+
float max = 4.0; // Infinity in our finite world is simple, let's just consider it 4
52+
float 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+
float abs = 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

Comments
 (0)