Skip to content

Update Mandelbrot.pde #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* The Mandelbrot Set
* by Daniel Shiffman.
*
* (slight modification by l8l)
*
* Simple rendering of the Mandelbrot set.
*/

Expand Down Expand Up @@ -43,21 +44,30 @@ for (int j = 0; j < height; j++) {
float x = xmin;
for (int i = 0; i < width; i++) {

// Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
// Now we test, as we iterate z = z^2 + c does z tend towards infinity?
float a = x;
float b = y;
int n = 0;
float max = 4.0; // Infinity in our finite world is simple, let's just consider it 4
float absOld = 0.0;
float convergeNumber = maxiterations; // this will change if the while loop breaks due to non-convergence
while (n < maxiterations) {
// We suppose z = a+ib
float aa = a * a;
float bb = b * b;
float twoab = 2.0 * a * b;
a = aa - bb + x;
b = twoab + y;
// Infinty in our finite world is simple, let's just consider it 16
if (dist(aa, bb, 0, 0) > 4.0) {
float abs = sqrt(aa + bb);
if (abs > max) { // |z| = sqrt(a^2+b^2)
// Now measure how much we exceeded the maximum:
float diffToLast = (float) (abs - absOld);
float diffToMax = (float) (max - absOld);
convergeNumber = n + diffToMax/diffToLast;
break; // Bail
}
float twoab = 2.0 * a * b;
a = aa - bb + x; // this operation corresponds to z -> z^2+c where z=a+ib c=(x,y)
b = twoab + y;
n++;
absOld = abs;
}

// We color each pixel based on how long it takes to get to infinity
Expand All @@ -66,11 +76,11 @@ for (int j = 0; j < height; j++) {
pixels[i+j*width] = color(0);
} else {
// Gosh, we could make fancy colors here if we wanted
float norm = map(n, 0, maxiterations, 0, 1);
float norm = map(convergeNumber, 0, maxiterations, 0, 1);
pixels[i+j*width] = color(map(sqrt(norm), 0, 1, 0, 255));
}
x += dx;
}
y += dy;
}
updatePixels();
updatePixels();