To code the mandelbrot set
, make two nested loop
from r = -1 to 1 and i = -1 to 1 by small increment
s, say 0.005. Map each (r,i) coordinate
to a pixel
(x,y) on the screen
Let z be a complex number. Then, for each pixel, count how many times you can square z and add (r,i) to make a new z, before z's absolute value is larger than e.g. 2. Make sure to put an upper bound on this loop, because for some combinations of (r,i), z's absolute value never rises above 2.
Use these formulas:
zr = zr2 - zi2 + r
zi = 2zrzi + i
Make sure you use the old zr in the second formula, and not the one you just calculated.
Now, pick the color of the pixel in question based on how many times the loop ilterated, and put it on the screen. Make the color black if your loop hit the upper bound.
Voila! After waiting a bit, you'll have a beautiful mandelbrot fractal on your screen.