-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.hs
134 lines (108 loc) · 3.53 KB
/
examples.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
-- Made by: Gagan Devagiri
-- For: Introduction To Computation: Tutorial 6
-- Date: 2nd November 2018
module LSystems where
import LSystem
import Data.Char
tree :: Int -> Command
tree x = f x
where
f 0 = GrabPen purple :#: Go 10
f x = g (x-1) :#: Branch (n :#: f (x-1))
:#: Branch (p :#: f (x-1))
:#: Branch (g (x-1) :#: f (x-1))
g 0 = GrabPen white :#: Go 10
g x = g (x-1) :#: g (x-1)
n = Turn 45
p = Turn (-45)
-- -- snowflake:
-- angle: 60
-- start: f--f--f-
-- rewrite: f → f+f--f+f
snowflake :: Int -> Command
snowflake x = f(x) :#: angle :#: angle :#: f(x) :#: angle :#: angle :#: f(x) :#: angle :#: angle
where
f 0 = GrabPen white :#: Go 10
f x = f(x-1) :#: angle' :#: f(x-1) :#: angle :#: angle :#: f(x-1) :#: angle' :#: f(x-1)
angle = Turn (-60)
angle' = Turn (60)
-- -- Peano-Gosper:
-- angle: 60
-- start: f
-- rewrite: f → f+g++g-f--ff-g+ g → -f+gg++g+f--f-g
peanoGosper x = f x
where
f 0 = GrabPen purple :#: Go 10
f x = f (x-1) :#: n :#: g (x-1) :#: n
:#: n :#: g (x-1) :#: p :#: f (x-1)
:#: p :#: p :#: f (x-1) :#: f (x-1)
:#: p :#: g (x-1) :#: n
g 0 = GrabPen white :#: Go 10
g x = p :#: f (x-1)
:#: n :#: g (x-1)
:#: g (x-1) :#: n
:#: n :#: g (x-1) :#: n
:#: f (x-1) :#: p :#: p
:#: f (x-1) :#: p :#: g (x-1)
n = Turn 60
p = Turn(-60)
--------------
-- Cross:
-- angle: 90
-- start: f-f-f-f
-- rewrite: f → f-f+f+ff-f-f+f
cross x = f x :#: n :#: f x :#: n :#: f x :#: n :#: f x
where
f 0 = GrabPen white :#: Go 10
f x = f (x-1) :#: n :#: f (x-1) :#: p
:#: f (x-1) :#: p :#: f (x-1)
:#: f (x-1) :#: n :#: f (x-1)
:#: n :#: f (x-1) :#: p :#: f (x-1)
n = Turn 90
p = Turn(-90)
--------
-- Branch:
-- angle: 22.5
-- start: g
-- rewrite: g → f-[[g]+g]+f[+fg]-g f → ff
branch x = g x
where
g 0 = GrabPen purple :#: Go 10
g x = f (x-1) :#: p :#: Branch (Branch (g (x-1))
:#: n :#: g (x-1)) :#: f (x-1)
:#: Branch (n :#: f (x-1) :#: g (x-1)) :#: p :#: g (x-1)
f 0 = GrabPen white :#: Go 10
f x = f (x-1) :#: f (x-1)
n = Turn 22.5
p = Turn(-22.5)
---------
-- 32-segment:
-- angle: 90
-- start: F+F+F+F
-- rewrite: F → -F+F-F-F+F+FF-F+F+FF+F-F-FF+ FF-FF+F+F-FF-F-F+FF-F-F+F+F-F+
thirtytwo x = f x :#: n :#: f x :#: n :#: f x :#: n :#: f x
where
f 0 = GrabPen white :#: Go 10.0
f x = p :#: f (x-1) :#: n
:#: f (x-1) :#: p :#: f (x-1)
:#: p :#: f (x-1) :#: n :#: f (x-1)
:#: n :#: f (x-1) :#: f (x-1) :#: p
:#: f (x-1) :#: n :#: f (x-1) :#: n
:#: f (x-1) :#: f (x-1) :#: n :#: f (x-1)
:#: p :#: f (x-1) :#: p :#: f (x-1) :#: f (x-1)
:#: n :#: f (x-1) :#: f (x-1) :#: p
:#: f (x-1) :#: f (x-1) :#: n :#: f (x-1) :#: n :#: f (x-1)
:#: p :#: f (x-1) :#: f (x-1) :#: p :#: f (x-1)
:#: p :#: f (x-1) :#: n :#: f (x-1)
:#: f (x-1) :#: p :#: f (x-1) :#: p :#: f (x-1)
:#: n :#: f (x-1) :#: n
:#: f (x-1) :#: p :#: f (x-1) :#: n
n = Turn 90
p = Turn (-90)
triangle :: Int -> Command
triangle x = p :#: f x
where
f 0 = Go 10
f x = f (x-1) :#: p :#: f (x-1) :#: n :#: f (x-1) :#: n :#: f (x-1) :#: p :#: f (x-1)
n = Turn 90
p = Turn (-90)