-- Michael Ichnowski, 0562947 -------------------------------------------------------------------- -- My picture for the competition is called 'leaf2'. -- To see it, type 'leaf2 13' at the prompt (without display!!!) -- Its quicker with i.e.'leaf2 11' but the effect is better with 13 -- You may also wish to have a look at rings : ring1,...,ring4 -- and at the 'leaf' (only names necessary at the prompt) -- Definitions are at the end. -------------------------------------------------------------------- import LSystem -- 1. join :: [Command] -> Command join coms= foldr1 (:#) coms -- 2. split :: Command -> [Command] split (com1 :# com2)= (split com1) ++(split com2) split com1 = [com1] -- 3. copy :: Int -> Command -> Command copy i com= join (replicate i com) -- 4. hexagon :: Distance -> Command hexagon dist= join (replicate 6 (Go dist :# Turn 60)) -- 5. polygon :: Distance -> Int -> Command polygon dist i= join (replicate i (Go dist :# Turn (360/(fromIntegral i)))) -- 6. spiral :: Distance -> Int -> Distance -> Angle -> Command spiral dist 1 step ang= if (dist > 0) then Go dist :# Turn ang else Sit spiral dist i step ang= if (dist > 0) then Go dist :# Turn ang :# (spiral (dist-step) (i-1) step ang) else Sit -- 7. optimise :: Command -> Command optimise com = join (optTest(split com)) opt :: [Command] -> [Command] opt (com: []) = [com] opt (Sit: rest) = rest opt (Go 0: rest) = opt rest opt (Turn 0: rest)= opt rest opt (Go x: Go y: rest) = opt (Go (x+y):rest) opt (Turn x: Turn y: rest) = opt (Turn (x+y): rest) opt (com1:com2:rest) = com1:opt(com2:rest) optTest :: [Command] -> [Command] optTest coms | coms == (opt coms) = coms | otherwise = optTest (opt coms) -- 8. arrowhead :: Int -> Command arrowhead x = f x where f 0 = Go 10 f(x+1)= g x :# p :# f x :# p :# g x g 0 = 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 f 0 = Go 10 f(x+1)= f x :# f x g 0 = 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 n =Turn 22.5 p =Turn (-22.5) -- 10. hilbert :: Int -> Command hilbert x = l x where l 0 = 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 = 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 =Go 10 n =Turn 90 :# GrabPen red p =Turn (-90) :# GrabPen blue ---------------------------------------------------------------------------------------------- splash :: Command -> Command splash com= join[(GrabPen col) :# com1|(col,com1)<- (zip (pal1 (root3 0 (fromIntegral(length (split com)))))(split com))] ---------------------------------------------------------------------------------------------- pal1 :: Int -> [Pen] pal1 n = [Colour ((fromIntegral r)/(fromIntegral n)) ((fromIntegral g)/(fromIntegral n))((fromIntegral b)/(fromIntegral n))| (r,g,b)<-(palette n)] ---------------------------------------------------------------------------------------------- palette :: Int -> [(Int,Int,Int)] palette n = [(r, g, b)|r <- [0..n], g <- [0..n], b <- [0..n]] ---------------------------------------------------------------------------------------------- root3 :: Int -> Int -> Int root3 x y | (x+1)*(x+1)*(x+1)>y = x | otherwise = root3 (x+1) y ---------------------------------------------------------------------------------------------- lea2 :: Distance -> Int -> Distance -> Angle -> Command lea2 dist 1 step ang= if (dist > 0) then Go dist :# Turn ang else Sit lea2 dist i step ang= if (dist > 0) then Go dist :# Branch (Turn 30 :# lea2 (dist/2) (i-1) step (ang*2/3)) :# Turn ang :# (lea2 (dist-step) (i-1) step ang) else Sit ---------------------------------------------------------------------------------------------- ring1 = display 1.0 (GrabPen Inkless :# Go (50) :# Turn 40 :# Go (-50) :# splash(optimise(spiral 150 1000 0.09 71))) ring2 = display 1.0 (GrabPen Inkless :# Go (50) :# Turn 40 :# Go (-50) :# splash(optimise(spiral 150 1000 0.09 53))) ring3 = display 1.0 (GrabPen Inkless :# Go (50) :# Turn 40 :# Go (-50) :# splash(optimise(spiral 150 1000 0.09 51))) ring4 = display 1.0 (GrabPen Inkless :# Go (50) :# Turn 40 :# Go (-50) :# splash(optimise(spiral 150 1300 0.09 57))) leaf = display 1.0 (splash(optimise(lea2 30 10 0.3 3))) ---------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------- leaf2 nSteps= display 1.0 ((optimise (join[(Branch(splash (Turn x :# lea2 33 nSteps 0.3 15) ))|x<-[0,60..300]]))) ---------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------