Mandelbrot Set Programming
The computer code
The coordinate system used to draw the Mandelbrot Set is represented with two axes. Remember, the complex number is in the form a + bi, a is represented on the x-axis, and b is represented on the y axis.
Wikipedia describes some of the mathematics to the Mandelbrot Set and has some pseudo code which you can base your program on. However, the pseudo code in Wikipedia is nearly unreadable because it is highly optimized.
Here is the gist:
1. Pick a point (obviously this will be the pixels on the screen) and convert it to a complex number c.
2. Set z[0] to c.
3. To get z[1] use the formula, z[n+1] = z[n] ^ 2 + c
4. Thus z[1] = z[0]^2 + c
5. You repeat this process about 1000 times to get z[1000] (higher numbers are required for deeper zooms)
6. After 1000 iterations, you check whether the complex number z[1000] is within a certain distance from the origin called the escape radius. This radius is usually a small number like 4 (though I used 40). If it escapes this radius, the program assumes it will go all the way into infinity. If it doesn't escape the radius, the program assumes it will never escape no matter how many iterations.
If you don't know how to calculate the radius, use the distance formula (a variation of the Pythagoras theorem. radius = sqrt(a^2 + b^2)
7. If it escaped, color the point white. If it didn't escape, color the point black. This is the basic Mandelbrot set.
Note: The Mandelbrot Set is situated around from -2 to 2 on the x-axis (a) and -2 to 2 on the y-axis (b).
Optimization: Rather than waiting to process the 1000th iteration, determine at each iteration if the point escaped because most of the points will escape much earlier.
The most challenging part is to add color the Mandelbrot Set. The color is derived from how long it took to break past the escape radius. Thus, the function that determines whether a point escapes or not should be modified to return the number of iterations it took to escape (return the maximum number of iterations if it doesn't escape at all). The best way is to experiment, modify and create new methods. The methods shown below should get you going, but just keep trying.
All colors are determined in RGB values. Thus 50% of red, would be 50% of the red, green and blue component. Remember, if it doesn't escape, it is always traditionally drawn black.
Let 0 iterations represent the color black.
Let the max iterations represent the color red.
Lets say max iterations is 1000, and it took 200 iterations to escape past the escape radius.
Take the ratio of iterations to max iterations (iterations/max iterations) which is 20% in this case. Thus the color should be 20% red.
Once you played with the starting algorithm, you will wish for more colors. The only difference here is to set more colors:
Let 0 iterations represent blue
Let 100 iterations represent red
Let 200 iterations represent orange
Let 300 iterations represent green
And so on...
If you get 200 iterations, the color is orange.
If you get 150 iterations, it is 50% red and 50% orange.
If you get 235, it is 65% orange and 35% green.
If you get 10, it is 90% blue and 10% red.
Remember to add colors together, you add the red, green and blue components separately.
Advanced Tips:
1. Experiment a lot and save your algorithms.
2. Play around with Photoshop gradients and insert multiple colors to get a feel for gradients and to quickly determine what you desire in your program.
3. This program actually uses a looping gradient because it was too hard to manually come up with a color scheme from 0 to 5000. Thus, the pictures shown in the gallery were made using only 4 colors from a range 0 to 100. These 4 colors are repeated constantly (100 to 200, 200 to 300). Sometimes the simple way is the best way.