20/12/2013 #generative-graphics #math #processing

I never managed to code this classic, the Mandelbrot set, but now I finally tried it out in processing.

It is not that complicated, if one understands what the image shows: For each given two-dimensional point a sequence is tested on how fast it tends towards infinity. The brighter a spot, the longer it takes. The sequence works with complex numbers, but if you understand them as two-dimensional coordinates there’s nothing complex about it. The sequence is

where c is a complex number, something like

The difference between two following elements of the sequence are determinated by the Euclidean distance. All you do is to count the number of iterations to when the distance between two consectutive elements of the sequence runs into infinity.

In the processing code below this is done by the *iterationsToInfinite()* function. For the screen coordinates (a, b) it will tell how many iterations are needed (limited by *MAX_ITERATIONS*) to tell if the sequence runs into infinite.

You can zoom in infinitely (use the *lowerbound*/*upperbound* variables), new structures will continuously show up. In the program the limit is the minimum resolution of the float data type.

int MAX_ITERATIONS = 1000; float lowerboundX = 0.00 - 0.0125; float upperboundX = 0.00 + 0.0125; float lowerboundY = -0.65 - 0.0125; float upperboundY = -0.65 + 0.0125; void setup() { size(1920, 1200); float min = MAX_ITERATIONS; float max = 1; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float t = iterationsToInfinite( map(x, 0, width, lowerboundX * width/height, upperboundX * width/height), map(y, 0, height, lowerboundY, upperboundY) ); if (t > max) max = int(t); if (t < min) min = int(t); } } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float t = iterationsToInfinite( map(x, 0, width, lowerboundX * width/height, upperboundX * width/height), map(y, 0, height, lowerboundY, upperboundY) ); set(x, y, color(map(t, min, max, 30, 255)) ); } } } int iterationsToInfinite(float a, float b) { float za1 = 0; float za2 = 0; float zb1 = 0; float zb2 = 0; int numOfIterations = 0; float difference = 0; for (int i = 0; i < MAX_ITERATIONS; i++) { za2 = sq(za1) - sq(zb1) + a; zb2 = 2*za1*zb1 + b; difference = sqrt(sq(za2 - za1) + sq(zb2 - zb1)); if (Float.isInfinite(difference)) { numOfIterations = i; break; } za1 = za2; zb1 = zb2; } return numOfIterations; }

Leave a Reply