How to Generate Escape Time Fractals

by Nihil Shah
June 19, 2013

Two years ago, I had a tumblr (therealnihil) where I would post fractals I generated using gnofract4d and some bash scripts. Unfortunately, I deleted it one night. A lot of the fractals are still accessible via reposts. Every one of the posts would link to a tutorial I wrote on how to generate escape time fractals. Here’s the text of that tutorial, slightly modified:

Escape-Time Fractals: Generating the Mandelbrot Set

I think I’ve shown enough proof of just how cool escape-time fractals can look. So I thought I would write a tutorial on how they are generated. I have a pseudocode example for the Mandelbrot set at the end of this post.

A large class of fractals (not just escape-time fractals) are generated by repeating a certain rule over, and over again. For example: the Cantor set is generated by splitting a line into three parts and erasing the middle part; then split into three and erase the middle part on the remaining two lines; then split into three and erase the middle part on the remaining four lines; and so on.

Escape-time fractals also repeat a rule over and over again—but instead of pencil movements—a formal formula is repeated for a certain number of iterations.

A escape time fractal is generated by continually repeating a formula for a certain number of iterations. If the formula’s output for a certain parameter (represented by c in the Mandelbrot set) is unbounded (it continues to approach infinity; well technically you can’t approach infinity, but it keeps getting bigger) then that parameter is not in that set. If the formula’s output for the parameter is bounded, it is in that set.

n = 1000
for each pixel on the screen:
    c = complex(pixel.x, pixel.y) 
    z = complex(0, 0)    
    i = 0

    while (i < n):
        z = z^2 + c
        if (|z| > 2): break
        i++

    if (i == n):
        color(pixel, 0xFFFFFF)
    else:
        color(pixel, i)

So in the above code, the formula \(z = z^{2} + c\) is repeated, if at some point during the 1000 iterations z’s magnitude (absolute value) exceeds 2, then break the loop; c isn’t in the Mandelbrot set. If it is in the Mandelbrot set, then color the pixel white. If it isn’t in the Mandelbrot set, color the pixel based on the number of iterations it took to escape. The larger the n, the more detailed the fractal is. There are tons of things to experiment with: the coloring algorithm, the bailout algorithm, the escape radius, the set’s formula itself, try every other pixel instead of every pixel…

Creative Commons License