-- Informatics 1 - Functional Programming
-- Tutorial 7
-- Kiss Roland
-- Week 9 - Due: 20/21 Nov.
import LSystem
import Test.QuickCheck
-- Exercise 1
-- 1a. split
split :: Command -> [Command]
split (Sit) = []
split (c :#: d) = (split c) ++ (split d)
split c = [c]
-- 1b. join
join :: [Command] -> Command
join [] = Sit
join [c] = c
join (c:cs) = c :#: join cs
--1c. testing join, split
prop_join_split c = split c == split (join (split c))
-- Exercise 2
-- 2a. copy
copy :: Int -> Command -> Command
copy n c = join (replicate n c)
-- 2b. pentagon
pentagon :: Distance -> Command
pentagon d = copy 5 c
where c = Go 50 :#: Turn 72
-- 2c. polygon
polygon d n = copy n (Go d :#: (Turn (360/(fromIntegral n))))
-- Exercise 3
-- spiral
spiral :: Distance -> Int -> Distance -> Angle -> Command
spiral side n step angle | n == 0 = Sit
| otherwise = (Go side :#: Turn angle) :#: (spiral (side-step) (n-1) step angle)
-- Exercise 4
-- optimise
optimise = undefined
-- L-Systems
-- 5. arrowhead
arrowhead :: Int -> Command
arrowhead x = p :#: 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)
-- 6. snowflake
snowflake :: Int -> Command
snowflake x = n :#: f x :#: n :#: n :#: f x :#: n :#: n :#: f x :#: n :#: n
where
f 0 = GrabPen brown :#: Go 10
f (x+1) = f x :#: p :#: f x :#: n :#: n :#: f x :#: p :#: f x
n = Turn 60
p = Turn (-60)
-- 7. hilbert
hilbert :: Int -> Command
hilbert x = p :#: 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 green :#: Go 10
r (x+1) = n :#: (l x :#: f x ) :#: p :#: (r x :#: f x :#: r x) :#: p :#: (f x :#: l x) :#: n
f 0 = GrabPen blue :#: Go 10
f x = GrabPen green :#: Go 10
n = Turn 90
p = Turn (-90)
peanoGosper :: Int -> Command
peanoGosper x = f x
where
f 0 = 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 = 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)
-------------------------------
-- The Entry for The Fractal Competition
-- Name: Kiss Roland
-- Matriculation number: s0833056
-- Just run "display image".
yellow = Colour 255 255 0
brown = Colour 0.5 0.25 0
inDirection :: Int -> Command
inDirection x = f x
where
f 0 = GrabPen green :#: Go 10
f (x+1) = f x :#: Branch (Turn angle :#: Go 100) :#: (Turn angle' :#: Go 10)
angle = 45
angle'= (-45)
inDirection' :: Distance -> Command
inDirection' x = Branch (Turn x :#: Go 10)
inDirection'' :: Distance -> Command
inDirection'' x = Branch (Turn x :#: Go 4)
inDirection''' :: Distance -> Command
inDirection''' x = Branch (Turn x :#: Go 1)
pattern :: Command
pattern = GrabPen yellow :#: join (map inDirection' [0.1,0.5..200]) :#: GrabPen red :#: join (map inDirection' [201,201.5..250 ]) :#: GrabPen yellow :#: join (map inDirection' [251,251.5..270 ]) :#: GrabPen brown :#: join (map inDirection' [271,271.5..320 ]) :#: GrabPen yellow :#: join (map inDirection' [321,321.5..340]) :#: GrabPen brown :#: join (map inDirection' [341,341.5..359.5]) :#: GrabPen black :#: join (map inDirection'' [2,4..1000])
image :: Command
image = pattern :#: GrabPen red :#: join (map inDirection' [0.1,0.7..200]):#:GrabPen yellow :#: join (map inDirection''' [1,5..360]) :#:GrabPen red :#: join (map inDirection''' [2,8..360])