-
Notifications
You must be signed in to change notification settings - Fork 4
/
turtlesamples.go
176 lines (154 loc) · 3.83 KB
/
turtlesamples.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package genlsystem
import (
"image"
"image/color"
"math/rand"
)
func Hilbert(n int) image.Image {
path := Lindenmayer([]string{"A"}, map[string][]string{
"A": {"-", "B", "F", "+", "A", "F", "A", "+", "F", "B", "-"},
"B": {"+", "A", "F", "-", "B", "F", "B", "-", "F", "A", "+"},
}, n)
turtle := NewTurtle(map[string]func(*Turtle){
"F": func(t *Turtle) {
t.Draw(5.0, 0)
},
"-": func(t *Turtle) {
t.Turn(-90)
},
"+": func(t *Turtle) {
t.Turn(90)
},
})
return turtle.Go(path)
}
func Tree(n int) image.Image {
segmentlength := 10.0
green := color.RGBA{0x60, 0xFF, 0x00, 0xFF}
black := color.RGBA{0x33, 0x33, 0x33, 0xFF}
path := Lindenmayer([]string{"leaf"}, map[string][]string{
"leaf": {"branch", "[", "<", "leaf", "]", "<>", "branch", "[", ">", "leaf", "]"},
"branch": {"trunk", "[", "<", "leaf", "]", "[", "<>", "branch", "]", "[", ">", "leaf", "]"},
"trunk": {"+", "trunk", "<>", "trunk", "-"},
"+": {"+", "+"},
"-": {"-", "-"},
}, n)
rmm := func(min, max float64) float64 {
return rand.Float64()*(max-min) + min
}
turtle := NewTurtle(map[string]func(*Turtle){
"leaf": func(t *Turtle) { // first gen, green leave
t.SetColor(green)
w := t.Width()
t.SetWidth(10 * rand.Float64())
t.Draw(segmentlength/2*rand.Float64(), 0)
t.SetWidth(w)
},
"branch": func(t *Turtle) { // second gen, black branch
t.SetColor(black)
//t.SetWidth(1)
t.Draw(segmentlength*rand.Float64(), 0)
},
"trunk": func(t *Turtle) { // second gen, black branch
t.SetColor(black)
//t.SetWidth(2)
t.Draw(segmentlength*rand.Float64(), 0)
},
"+": func(t *Turtle) { // thicken
t.SetWidth(t.Width() + 0.25)
},
"-": func(t *Turtle) { // thinning
// w := t.Width() - 1
// if w < 1 { w = 1 }
t.SetWidth(t.Width() - 0.25)
},
"<": func(t *Turtle) { // turn left
t.Turn(rmm(-45, 0))
},
">": func(t *Turtle) { // turn right
t.Turn(rmm(0, 45))
},
"<>": func(t *Turtle) { // wiggle
t.Turn(rmm(-2, 2))
},
"[": func(t *Turtle) { // push position and angle
t.Save()
},
"]": func(t *Turtle) { // pop position and angle
t.Restore()
},
})
turtle.SetColor(black)
turtle.SetWidth(1)
turtle.Turn(-90)
return turtle.Go(path)
}
func BinTree(n int) image.Image {
segmentlenght := 1.0
green := color.RGBA{0x33, 0xFF, 0x33, 0xFF}
black := color.RGBA{0x00, 0x00, 0x00, 0xFF}
path := Lindenmayer([]string{"0"}, map[string][]string{
"1": {"1", "1"},
"0": {"1", "[", "0", "]", "0"},
}, n)
turtle := NewTurtle(map[string]func(*Turtle){
"0": func(t *Turtle) {
// draw a line segment ending in a leaf
t.SetColor(green)
t.SetWidth(5)
t.Draw(segmentlenght, 0)
},
"1": func(t *Turtle) {
// draw a line segment
t.SetColor(black)
t.SetWidth(1)
t.Draw(segmentlenght, 0)
},
"[": func(t *Turtle) {
// push position and angle, turn left 45 degrees
t.Save()
t.Turn(-45)
},
"]": func(t *Turtle) {
// pop position and angle, turn right 45 degrees
t.Restore()
t.Turn(45)
},
})
turtle.Turn(-90)
return turtle.Go(path)
}
func Plant(n int) image.Image {
segmentlength := 4.0
green := color.RGBA{0x60, 0xFF, 0x00, 0xFF}
path := Lindenmayer([]string{"X"}, map[string][]string{
"X": {"F", "-", "[", "[", "X", "]", "+", "X", "]", "+", "F", "[", "+", "F", "X", "]", "-", "X"},
"F": {"F", "F"},
}, n)
turtle := NewTurtle(map[string]func(*Turtle){
"F": func(t *Turtle) {
// draw forward
t.Draw(segmentlength*rand.Float64(), 0)
},
"-": func(t *Turtle) {
// turn left 25°
t.Turn(-20 + rand.Float64()*10)
},
"+": func(t *Turtle) {
// turn right 25°
t.Turn(20 + rand.Float64()*10)
},
"[": func(t *Turtle) {
// push position and angle
t.Save()
},
"]": func(t *Turtle) {
// pop position and angle
t.Restore()
},
})
turtle.SetColor(green)
turtle.SetWidth(1)
turtle.Turn(-90)
return turtle.Go(path)
}