Fractals (Jeff Bezanson, Daniel Jones)

Original (second example from Daniel Jones's page on Compose): http://beowulf.csail.mit.edu/18.337/fractals.ipynb

This notebook illustrates three ways to obtain objects exhibiting fractal geometry:

  1. Approaching a pointset with a fractal boundary, using deterministic iteration.
  2. Explicit recursive subdivision.
  3. Iterated function systems, using the random iteration algorithm.

I will use three different graphics libraries to do this: PyPlot (calling Python's matplotlib), Compose.jl (which is great for nested geometry), and Cairo (which can draw thousands of primitives in no time).

Julia set


In [1]:
using PyPlot


INFO: Loading help data...

In [2]:
# julia set
# (the familiar mandelbrot set is obtained by setting c==z initially)
function julia(z, c; maxiter=200)
    for n = 1:maxiter
        if abs2(z) > 4
            return n-1
        end
        z = z*z + c
    end
    return maxiter
end


Out[2]:
julia (generic function with 1 method)

In [3]:
# varying the second argument to julia() tiny amounts results in a stunning variety of forms
@time m = [ uint8(julia(complex(r,i), complex(-.06,.67))) for i=1:-.002:-1, r=-1.5:.002:1.5 ];


elapsed time: 0.243365055 seconds (1502848 bytes allocated)

In [4]:
# the notebook is able to display ColorMaps
get_cmap("RdGy")


Out[4]:
RdGy

In [5]:
imshow(m, cmap="RdGy", extent=[-1.5,1.5,-1,1])


Out[5]:
PyObject <matplotlib.image.AxesImage object at 0x10cbd5310>

Sierpinski triangle


In [7]:
using Compose

function sierpinski(n)
    if n == 0
        compose(context(), polygon([(1,1), (0,1), (1/2, 0)]))
    else
        t = sierpinski(n - 1)
        compose(context(),
                (context(1/4,   0, 1/2, 1/2), t),
                (context(  0, 1/2, 1/2, 1/2), t),
                (context(1/2, 1/2, 1/2, 1/2), t))
    end
end

img = SVG("sierpinski.svg", 4inch, 4(sqrt(3)/2)inch)
draw(img, compose(sierpinski(8), linewidth(0.1mm), fill(nothing), stroke("black")))

In [29]:
# get more detail by scaling up
draw(SVG(9inch,7inch), compose(sierpinski(8), fill(nothing), linewidth(0.1mm)))