-- Kyle Shearer, s0567784 import LSystem import List -- 1. join :: [Command] -> Command join xs = foldr (:#) (Sit) xs -- 2. split :: Command -> [Command] split (x :# y) = (split x) ++ (split y) split x = [x] -- 3. copy :: Int -> Command -> Command copy x xs = join(replicate x xs) -- 4. hexagon :: Distance -> Command hexagon x = copy 6 (Go x :# Turn 60.0) -- 5. polygon :: Distance -> Int -> Command polygon x y | y == 6 = hexagon x | otherwise = copy y (Go x :# Turn (fromIntegral (360 `div` y))) -- 6. spiral :: Distance -> Int -> Distance -> Angle -> Command spiral a b c d | b > 0 = (Go a :# Turn d) :# (spiral (a - c) (b - 1) c d) | b <= 0 = (Sit) -- 7. optimise :: Command -> Command optimise (Go (x) :# Go (y)) = optimise (Go ((x) + (y))) optimise (Turn (x) :# Turn (y)) = optimise (Turn ((x) + (y))) optimise (x :# Sit) = optimise x optimise (Sit :# x) = optimise x optimise (x :# y :# z) | z == (Turn 0) || z == (Go 0) = (optimise ((optimise x) :# (optimise y))) | otherwise = optimise ((optimise x) :# (optimise ((optimise y) :# (optimise z)))) optimise (x :# y) | y == (Turn 0) || y == (Go 0) = (optimise x) | x == (Turn 0) || x == (Go 0) = (optimise y) | otherwise = (optimise x) :# (optimise y) optimise x = x -- 8. arrowhead :: Int -> Command arrowhead x = f x where f 0 = GrabPen blue :# Go 10 f (x+1) = g x :# n :# f x :# n :# g x g 0 = GrabPen blue :# Go 10 g (x+1) = f x :# p :# g x :# p :# f x p = Turn (-60) n = Turn 60 -- 9. branch :: Int -> Command branch x = g x where g 0 = GrabPen green :# Go 10 g (x+1) = f x :# n :# Branch (Branch (p :# g x) :# 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 = r x where l 0 = GrabPen green :# Go 10 l (x+1) = n :# r x :# f x :# p :# l x :# f x :# l x :# p :# f x :# r x :# n r 0 = GrabPen green :# Go 10 r (x+1) = p :# l x :# f x :# n :# r x :# f x :# r x :# n :# f x :# l x :# p f 0 = GrabPen green :# Go 10 f (x+1) = Go 10 n = Turn 90 p = Turn (-90) -- My not so exciting competition entry :) face :: Command face = ((polygon 12 30) :# Turn 90 :# Go 50 :# Turn 270 :# GrabPen white :# Go 30 :# GrabPen black :# Turn 180 :# (polygon 3 30) :# Turn 90 :# GrabPen white :# Go 20 :# GrabPen black :# (spiral 1 100 0.01 30))