Fractals in Haskell

Escape-time fractals created with Haskell and GD.

The full source repository is available using darcs. The algorithm for each fractal type is shown next to an example image.

Mandelbrot

drawMandelbrot :: Coordinate -> Color
drawMandelbrot (x,y) = colorIterations $ mandelbrot (x :+ y) (0 :+ 0) 0

mandelbrot :: Complex Double -- Coordinate to test
           -> Complex Double -- Iterating Z value
           -> Int -- Current iteration
           -> Int -- Iterations before diverging
mandelbrot c z iter
    | iter > maxIter = 0
    | otherwise = let z' = z^2 + c in
                  if magnitude(z') > 2
                  then iter
                  else mandelbrot c z' (iter+1)
	  

Julia Set

drawJulia :: Coordinate -> Color
drawJulia (x,y) = colorIterations $ julia c (x :+ y) 0

julia :: Complex Double -- Constant C
      -> Complex Double -- Iterating Z value
      -> Int -- Current iteration
      -> Int -- Iterations before diverging
julia c z iter
    | iter > maxIter = 0
    | otherwise = let z' = z^2 + c in
                  if magnitude(z') > 2
                  then iter
                  else julia c z' (iter+1)
	  

Burning Ship

drawShip :: Coordinate -> Color
drawShip (x,y) = colorIterations $ ship (x :+ y) (0 :+ 0) 0

ship :: Complex Double -- Coordinate to test 
           -> Complex Double -- Iterating Z value
           -> Int -- Current iteration
           -> Int -- Iterations before diverging
ship c z iter
    | iter > maxIter = 0
    | otherwise = let z' = (abs(realPart(z)) :+ abs(imagPart(z)))^2 + c in
                  if magnitude(z') > 2
                  then iter
                  else ship c z' (iter+1)
	  

Newton's Method

drawNewton :: Coordinate -> Color
drawNewton (x,y) = colorIterations $ newton (x :+ y) 0

newton :: Complex Double -- Coordinate to test
       -> Int -- Current iteration
       -> Int -- Iterations before diverging
newton z iter
    | iter > maxIter = 0
    | ( abs $ magnitude(r) - magnitude(z)) > minDiff = newton r (iter + 1)
    | otherwise = iter
    where r = z - (f z) / (f' z)
	  

©2008 Greg Heartsfield