-- John Novosel 0563409 -- type competition to see entry import LSystem -- 1. join :: [Command] -> Command join xs = foldr1 (:#) xs -- 2. split :: Command -> [Command] split xs = reverse (split' xs) split' :: Command -> [Command] split' ( x :# xs) = xs: (reverse (split x)) split' x = x:[] -- 3. copy :: Int -> Command -> Command copy x ys = join (replicate x ys) -- 4. hexagon :: Distance -> Command hexagon x = copy 6 (Go x :# Turn 60) -- 5. polygon :: Distance -> Int -> Command polygon x y = copy y (Go x :# Turn (fromIntegral (div 360 y))) -- 6. spiral :: Distance -> Int -> Distance -> Angle -> Command spiral a b c d = join [Go (a - (c * (fromIntegral x))) :# Turn d | x <- [0..(b - 1)]] -- 7. optimise :: Command -> Command optimise x = join (f (join (optimise' (f x)))) optimise' :: [Command] -> [Command] optimise' [] = [] optimise' (Go x:Go y:xs) = optimise' (Go (x+y):xs) optimise' (Turn x:Turn y:xs) = optimise' (Turn (x+y):xs) optimise' (Turn x:Go y:xs) = Turn x: optimise' (Go y:xs) optimise' (Go x:Turn y:xs) = Go x: optimise' (Turn y:xs) optimise' (x:xs) = x:optimise' xs f :: Command -> [Command] f x = [y | y <- (split x), y /= Sit, y /= Go 0, y /= Turn 0] -- 8. arrowhead :: Int -> Command arrowhead x = f x where f 0 = GrabPen red :# Go 10 f (x+1) = g x :# p :# f x :# p :# g x g 0 = GrabPen blue :# Go 10 g (x+1) = f x :# n :# g x :# n :# f x n = Turn 60 p = Turn (-60) -- 9. branch :: Int -> Command branch x = g x where g 0 = GrabPen red :# Go 10 g (x+1) = f x :# n :# Branch ( Branch (g x) :# p :# g x) :# p :# f x :# Branch (p :# f x :# g x) :# n :# g x f 0 = GrabPen blue :# Go 10 f (x+1) = f x :# f x n = Turn 22.5 p = Turn (-22.5) -- 10. hilbert :: Int -> Command hilbert x = l x where l 0 = GrabPen red :# Go 10 l (x+1) = p :# r x :# f x :# n :# l x :# f x :# l x :# n :# f x :# r x :# p r 0 = GrabPen blue :# Go 10 r (x+1) = n :# l x :# f x :# p :# r x :# f x :# r x :# p :# f x :# l x :# n f x = GrabPen green :# Go 10 n = Turn 90 p = Turn (-90) bush :: Int -> Command bush x = f x where f 0 = GrabPen blue :# Go 10 f (x+1) = g x :# n :# Branch (Branch (f x) :# p :# f x) :# p :# g x :# Branch (p :# g x :# f x) :# n :# f x g 0 = GrabPen red :# Go 10 g (x+1) = g x :# g x n = Turn 22.5 p = Turn (-22.5) comp :: Command comp = GrabPen Inkless:# Turn (-160) :# (copy 18 ((bush 5) :# Turn 160 :# GrabPen Inkless :# Go 450 :# Turn (-45))) competition = display 0.3 comp