-- -- Sierpinski's Gasket, Adapted from SOE. -- -- The SOE version used the imperative Draw monad to render the picture -- on the screen directly. Here we compose a Path that represents the -- image, and use that path to produce a Picture. -- -- This version is quite inefficient, however, because it isn't -- tail-recursive. -- module Sierpinski where import Haven -- Given a point and a width for both sides, form an equilateral right -- triangle: rightTri :: Point -> Double -> Path rightTri pt0 size = let (x,y) = pointXY pt0 in polygon [pt0, point (x+size) y, point x (y-size)] triTest :: Picture triTest = withColor blue $ picFill (rightTri (point 250 250) 50) minSize :: Double minSize = 16 sierpinskiTri :: Point -> Double -> Path sierpinskiTri pt size = if size <= minSize then rightTri pt size else let size2 = size / 2 (x,y) = pointXY pt t1 = sierpinskiTri pt size2 t2 = sierpinskiTri (point x (y-size2)) size2 t3 = sierpinskiTri (point (x+size2) y) size2 in pathAddShape t3 False (pathAddShape t2 False t1) sierpinski :: Picture sierpinski = let spath = sierpinskiTri (point 250 250) 500 in place origin $ withColor blue $ picFill spath