Sierpinski’s classic fractal.
import Diagrams.Backend.SVG.CmdLine
{-# LANGUAGE NoMonomorphismRestriction #-}
import Diagrams.Prelude
In the original version from Brent, fractals are placed in a triangular fashion and use recursion :
-- sierpinski 1 = triangle 1
-- sierpinski n = s
-- ===
-- (s ||| s) # centerX
-- where s = sierpinski (n-1)
Another version allow to change color at each level :
import Diagrams.Prelude
import Data.Colour.Palette.BrewerSet
clrs :: [Colour Double]
= brewerSet Purples 9
clrs
sierpinski :: Int -> [Colour Double] -> Diagram B
= go n <> triangle (2^n) # fc (clrs !! 0) # lw none
sierpinski n c where
= if null c then repeat black else cycle c
clrs
go n| n == 1 = t1 # fc (clrs !! 1)
| otherwise = appends tri (zip vecs (replicate 3 sierp))
where
= scale (2 ^ (n-1)) $ t1 # fc (clrs !! (n+1))
tri = [unitY, (rotateBy (-1/12) unitX), (rotateBy (1/12) unit_X)]
vecs = go (n-1)
sierp = triangle 1 # reflectY
t1
= sierpinski 7 clrs # lw none # center # frame 2 example
= mainWith (example :: Diagram B) main