A discretized version of the familiar Mandelbrot set. Adapted from code written by MathematicalOrchid.
import Diagrams.Backend.SVG.CmdLine
{-# LANGUAGE NoMonomorphismRestriction #-}
-- Adapted from: "The MathematicalOrchid, 24 Feb 2007"
-- http://warp.povusers.org/MandScripts/haskell.html
import Data.Complex
import Diagrams.Prelude hiding (magnitude,image)
Code to compute orbits of complex numbers under the Mandelbrot transformation, and decide on the magnitude of a pixel based on how slowly its orbit diverges.
= z*z + c
quadratic c z
critical_orbit :: Complex Double -> [Complex Double]
= iterate (quadratic z) 0
critical_orbit z
= length . takeWhile (\z -> magnitude z <= 2) . take maxIter
pixel = 32
maxIter = 128 edge
Generate a grid of points of the desired size.
=
side n v0 v1 let sv = (v1 - v0) / fromIntegral n
in [v0, (v0 + sv) .. v1]
= side edge (-2) 2
sideX = side edge (-2) 2
sideY
= map (\y -> map (:+ y) sideX) sideY grid
Generate the Mandelbrot image as a grid of pixel magnitudes.
= map (map (toSquare . pixel . critical_orbit)) grid image
To lay out the pixels in a grid we have to make them into a square whose opacity varies with the square root of the pixel value.
= square 1 # lw medium # fc black # opacity (sqrt o)
toSquare n where o = fromIntegral n / maxIter
= (vcat . map hcat $ image) # bgFrame 3 pink example
= mainWith (example :: Diagram B) main