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:
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).
In [1]:
using PyPlot
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]:
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 ];
In [4]:
# the notebook is able to display ColorMaps
get_cmap("RdGy")
Out[4]:
In [5]:
imshow(m, cmap="RdGy", extent=[-1.5,1.5,-1,1])
Out[5]:
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)))