-- Work by: Anna Leshinskaya s0674403 and Aditi Kalra s0676351 --Submission for COMPETITION by Anna Leshinskaya. Please run to see submission. module As7 where import LSystem --1. join join :: [Command] -> Command join [] = Sit join (l:ls) = foldl (:#) l ls --2. split split :: Command -> [Command] split (c :# d) = (split c) ++ (split d) split c = [c] --3. copy copy :: Int -> Command -> Command copy 1 n = n copy c n = join [n :# copy (c-1) n] -- hexagon hexagon :: Distance -> Command hexagon d = copy 6 (Go d :# Turn 60) -- 4. pentagon pentagon :: Distance -> Command pentagon d = copy 5 (Go d :# Turn 72) --5. polygon polygon :: Distance -> Int -> Command polygon d n = copy n (Go d :# (Turn (360/(fromIntegral n)))) --6. spiral spiral :: Distance -> Int -> Distance -> Angle -> Command spiral max nls step angle |nls==0 = Sit |otherwise = ( Go max :# Turn angle ) :# (spiral ( max-step) (nls-1) step angle) --7. optimise optimise :: Command -> Command optimise Sit = Sit optimise cm |checkOpt (split cm) (optimise2 (optimise1(split cm))) = cm | otherwise = optimise (join(optimise2 (optimise1(split cm)))) optimise1 :: [Command] -> [Command] optimise1 [Sit] = [Sit] optimise1 [] = [] optimise1 cs = [ c | c <- cs, c/=Sit && c/= Go 0 && c/= Turn 0] checkOpt :: [Command] -> [Command]-> Bool checkOpt a b = (length a) == (length b) optimise2 :: [Command] -> [Command] optimise2 [x] =[x] optimise2 [] = [] optimise2 (Turn x:Turn y:xs) = [Turn (x+y)] ++ optimise2 xs optimise2 (Go x: Go y:xs) = Go(x+y):(optimise2 xs) optimise2 (Go x: Turn y:xs)= Go x:(optimise2 (Turn y:xs)) optimise2 (Turn x: Go y:xs)= Turn x:(optimise2 (Go y:xs)) --8. arrowhead arrowhead :: Int -> Command arrowhead x = f x where f 0 = GrabPen red :# Go 10 f (x+1) = g x :# n :# f x :# n :# g x g 0 = GrabPen green :# Go 10 g (x+1) = f x :# p :# g x :# p :# f x n = Turn 60 p = Turn (-60) --9. snowflake snowflake :: Int -> Command snowflake x = f x :# p :# p :# f x :# p :# p :# f x :# p :# p where f 0 = GrabPen blue :# Go 10 f (x+1) = f x :# n :# f x :# p :# p :# f x :# n :# f x n = Turn 60 p = Turn (-60) --10. peanoGosper :: Int -> Command peanoGosper x = f x where f 0 = GrabPen maroon :# Go 10 f (x+1) = f x :# n :# g x :# n :# n :# g x :# p :# f x :# p :# p :# f x :# f x :# p :# g x :# n g 0 = GrabPen maroon :# Go 10 g (x+1) = p :# f x :# n :# g x :# g x :# n :# n :# g x :# n :# f x :# p :# p :# f x :# p :# g x n = Turn 60 p = Turn (-60) --Pracise/Bonus l-systems bush :: Int -> Command bush x = f x where f 0 = GrabPen purple :# Go 10 f (x+1) = g x :# n :# Branch ( Branch (f x) :# Branch p :# f x) :# p :# g x :# Branch (p :# g x :# f x) :# n :# f x g 0 = GrabPen turquoise :# Go 10 g (x+1) = g x :# g x n = Turn (-22.5) p = Turn 22.5 circle :: Int -> Command circle x = f x where f 0 = GrabPen drkgrn f 1 = GrabPen beige f (x+1) = n :# n :# f 1 :# m :# f 0 :# l :# f x n = Turn 20 m = Turn 30 l = Turn 40 colourspiral :: Pen -> Command colourspiral p = GrabPen p :# (spiral 25 300 0.1 9) polygoncircle ::Int -> Float -> Command polygoncircle x y = f x y where t1 = n :# GrabPen pink :# polygon y 4 t2 = n :# GrabPen turquoise :# polygon y 4 m = Turn (c) n = GrabPen white :# Go (b) f 0 _ = Sit f (x+1) y = t1 :# m :# t2 :# m :# f x y b = (2*y) c = (4*y) pretty :: Int -> Int -> Float -> Command pretty x y z = copy x (polygoncircle y z) polygonsquare ::Int -> Float -> Command polygonsquare x y = f x y where t1 = n :# GrabPen pink :# polygon y 2 t2 = n :# GrabPen turquoise :# polygon y 2 m = Turn 90 n = GrabPen white :# Go (b/2) f 0 _ = Sit f (x+1) y = t1 :# t2 :# t1 :# t2 :# t1 :# t2 :# m :# f x y b = (2*y) c = (4*y) --Submission for Competition body :: Command body = GrabPen white :# Go 30 :# GrabPen maroon :# m :# m :# m :# n :# n :# n :# n :# o :# o :# m :# m :# o :#m :# n :# m :# m where m = Go 25 :# Turn (-10) n = Go 25 :# Turn (-20) o = Go 25 :# Turn (-30) p = Go 10 :# Turn (-50) a = Go 10 :# Turn (10) b = Go 10 :# Turn (20) c = Go 10 :# Turn (30) stail :: Int -> Command stail x = f x where f 0 = GrabPen purple :# Go 10 f (x+1) = n :# Branch ( Branch (n :# f x ) :# Branch n :# f x :# Branch n:# f x :# ( Branch n :# f x :# f x) :# Branch n :# g x :# ( Branch n :# f x)) :# n :# g x :# Branch (n :# n :# g x :# p :# f x) :# p :# f x g 0 = GrabPen drkgrn :# Go 10 g (x+1) = g x :# g x n = Turn (-22.5) p = Turn 22.5 bird :: Command bird = Turn (-90) :# (body ) :# neck :# GrabPen white :# Go 120 :# Turn (30) :# (stail 3) :# GrabPen white :# Turn 160:# Go 255 :# Turn 90 :# Go 170 :# eye :# GrabPen white :# Go 15 :# Turn 65 :# Go 3 :# beak :# Turn (-65) neck :: Command neck = Turn (360) :# m :# m :# m :# m :# a :# a :# a :# b :# c :# c :# o :# a :# Turn (-60) :# Go 2 :# Turn (-40) :# Go 5 :# q :# q :# q :# q :# q :# q :# q :# q :# q :# q :# q :#q :# a :# q :# a :# q :# q :# r :# r :# r :# a :# a :# b :# b :# b :# b where m = Go 25 :# Turn (-10) n = Go 25 :# Turn (-20) o = Go 25 :# Turn (-30) p = Go 10 :# Turn (-50) a = Go 10 :# Turn (10) b = Go 10 :# Turn (20) c = Go 10 :# Turn (30) d = Go 10 :# Turn (40) q = Go 5 :# Turn (-10) r = Go 10 :#Turn (-15) eye :: Command eye = GrabPen turquoise :# (polygon 2 7) beak :: Command beak = GrabPen beige :# (polygon 9 3) turquoise, pink, purple, maroon, beige, drkgrn :: Pen turquoise = Colour 0.0 0.5 0.5 pink = Colour 0.9 0.2 0.3 purple = Colour 0.5 0.1 0.6 maroon = Colour 0.6 0.1 0.2 drkgrn = Colour 0.2 0.5 0.0 beige = Colour 0.8 0.6 0.3 mix :: Pen -> Pen -> Pen mix (Colour a b c) (Colour a' b' c') = Colour ((a+a') / 2) ((b+b') / 2) ((c+c') / 2)