Skip to content

Commit 2946811

Browse files
author
l8l
authored
Update Mandelbrot.pde
With the proposed changes, the plot of the Mandelbrot set will be smoother than before. This is achieved by measuring how much the maximum threshold was exceeded.
1 parent 070a6e0 commit 2946811

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

  • content/examples/Topics/Fractals and L-Systems/Mandelbrot

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+
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

Comments
 (0)