1
1
/**
2
2
* The Mandelbrot Set
3
3
* by Daniel Shiffman.
4
- *
4
+ * (slight modification by l8l)
5
+ *
5
6
* Simple rendering of the Mandelbrot set.
6
7
*/
7
8
@@ -43,21 +44,30 @@ for (int j = 0; j < height; j++) {
43
44
float x = xmin;
44
45
for (int i = 0 ; i < width ; i++ ) {
45
46
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?
47
48
float a = x;
48
49
float b = y;
49
50
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
50
54
while (n < maxiterations) {
55
+ // We suppose z = a+ib
51
56
float aa = a * a;
52
57
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;
58
64
break ; // Bail
59
65
}
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;
60
69
n++ ;
70
+ absOld = abs;
61
71
}
62
72
63
73
// 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++) {
66
76
pixels [i+ j* width ] = color (0 );
67
77
} else {
68
78
// 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 );
70
80
pixels [i+ j* width ] = color (map (sqrt (norm), 0 , 1 , 0 , 255 ));
71
81
}
72
82
x += dx;
73
83
}
74
84
y += dy;
75
85
}
76
- updatePixels ();
86
+ updatePixels ();
0 commit comments