Skip to content

Instantly share code, notes, and snippets.

@jhenahan
Created April 2, 2012 11:33
Show Gist options
  • Save jhenahan/2282833 to your computer and use it in GitHub Desktop.
Save jhenahan/2282833 to your computer and use it in GitHub Desktop.
Heighway Dragon - byorgey
{- Heighway dragon. See
http://en.wikipedia.org/wiki/Dragon_curve -}
module Main where
import Graphics.Rendering.Diagrams
import Control.Monad.State
import Data.Maybe
dragonStr :: Int -> String
dragonStr 0 = "FX"
dragonStr n = concatMap rules $ dragonStr (n-1)
where rules 'X' = "X+YF+"
rules 'Y' = "-FX-Y"
rules c = [c]
strToPath :: String -> Path
strToPath s = pathFromVectors . catMaybes $ evalState c (0,-1)
where c = mapM exec s
exec 'F' = Just `fmap` get
exec '-' = modify left >> return Nothing
exec '+' = modify right >> return Nothing
exec _ = return Nothing left (x,y) = (-y,x) right (x,y) = (y,-x)
dragon :: Int -> Diagram
dragon = lc red . curved 0.8 . strToPath . dragonStr
main = renderAs PNG "dragon.png" (Width 300) (dragon 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment