2d fractals

Fractals are cool! In contrast to what you might think, they are really easy to generate. You just have to iterate simple formulas. Lets take the most famous fractal as an example, the mandelbrot set. The iterative formula that generates the mandelbrot set is:

$$ z_{i+1} = z_i^2 + c $$

where \(c\) is a complex number of which the real and imaginary part are respectively the x and y-coordinate on the screen. Every iteration starts with \( z_0 = c \). You just iterate this \(n\) times for every pixel on the screen. Here is the code in python (as an example) that gives you a value between 0 and 1 for a given x and y coordinate:

def mandelbrot(z, n):
    c = z
    for i in range(maxiter):
        if abs(z) > 2:
            return i
        z = z*z + c
    return n

Map these results to a pretty colormap, and voila! Beautiful patterns. Here is a video created with code similar to the code above:

Here is a little fractal 'explorer' I made in javascript. You can choose different fractals. Click and drag to move, scroll to zoom in/out (or press space to continuously zoom in). The javascript code can be found in the source code of this page.