/
Turtle.jl
268 lines (219 loc) · 6.41 KB
/
Turtle.jl
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
export Turtle,
Forward,
Turn,
Pendown,
Penup,
Circle,
Orientation,
Rectangle,
Push,
Pop,
Message,
HueShift,
Randomize_saturation,
Pencolor,
Penwidth,
Pen_opacity_random,
Reposition,
Towards
"""
Turtle()
Turtle(O)
Turtle(0, 0)
Turtle(O, pendown=true, orientation=0, pencolor=(1.0, 0.25, 0.25))
Create a Turtle. You can command a turtle to move and draw "turtle graphics".
The commands (unusually for Julia) start with a capital letter, and angles are specified in degrees.
Basic commands are `Forward()`, `Turn()`, `Pendown()`, `Penup()`, `Pencolor()`,
`Penwidth()`, `Circle()`, `Orientation()`, `Rectangle()`, and `Reposition()`.
Others include `Push()`, `Pop()`, `Message()`, `HueShift()`, `Randomize_saturation()`,
`Reposition()`, and `Pen_opacity_random()`.
"""
mutable struct Turtle
xpos::Float64
ypos::Float64
pendown::Bool
orientation::Float64 # stored in radians but set in degrees
pencolor::Tuple{Float64, Float64, Float64} # it's an RGB turtle
function Turtle(xpos, ypos, pendown::Bool, orientation, pencolor=(0.0, 0.0, 0.0))
new(xpos, ypos, pendown, orientation, pencolor)
end
end
Turtle(x, y) = Turtle(x, y, true, 0, (0, 0, 0))
Turtle(pos::Point=O) = Turtle(pos.x, pos.y, true, 0, (0, 0, 0))
Turtle(pos::Point, pendown::Bool) = Turtle(pos.x, pos.y, pendown, 0, (0, 0, 0))
Turtle(pos::Point, pendown::Bool, orientation) = Turtle(pos.x, pos.y, pendown, orientation, (0, 0, 0))
Turtle(pos::Point, pendown::Bool, orientation, col::NTuple{3, Number}) = Turtle(pos.x, pos.y, pendown, orientation, col)
Turtle(pos::Point, pendown::Bool, orientation, r, g, b) = Turtle(pos.x, pos.y, pendown, orientation, (r, g, b))
Base.broadcastable(t::Turtle) = Ref(t)
# a stack to hold pushed/popped turtle positions
const queue = Array{Array{Float64, 1}, 1}()
"""
Forward(t::Turtle, d=1)
Move the turtle forward by `d` units. The stored position is updated.
"""
function Forward(t::Turtle, d=1)
oldx, oldy = t.xpos, t.ypos
t.xpos += (d * cos(t.orientation))
t.ypos += (d * sin(t.orientation))
if t.pendown
gsave()
sethue(t.pencolor...)
line(Point(oldx, oldy), Point(t.xpos, t.ypos), :stroke)
grestore()
end
end
"""
Turn(t::Turtle, r=5.0)
Increase the turtle's rotation by `r` degrees. See also `Orientation`.
"""
function Turn(t::Turtle, r=5.0)
t.orientation = mod2pi(t.orientation + deg2rad(r))
end
"""
Orientation(t::Turtle, r=0.0)
Set the turtle's orientation to `r` degrees. See also `Turn`.
"""
function Orientation(t::Turtle, r=0.0)
t.orientation = mod2pi(deg2rad(r))
end
"""
Towards(t::Turtle, pos::Point)
Rotate the turtle to face towards a given point.
"""
function Towards(t::Turtle, pos::Point)
x, y = pos
dx = x - t.xpos
dy = y - t.ypos
t.orientation = atan(dy, dx)
end
"""
Pendown(t::Turtle)
Put that pen down and start drawing.
"""
function Pendown(t::Turtle)
t.pendown = true
end
"""
Penup(t::Turtle)
Pick that pen up and stop drawing.
"""
function Penup(t::Turtle)
t.pendown = false
end
"""
Circle(t::Turtle, radius=1.0)
Draw a filled circle centered at the current position with the given radius.
"""
function Circle(t::Turtle, radius=1.0)
gsave()
sethue(t.pencolor...)
circle(t.xpos, t.ypos, radius, :fill)
grestore()
end
"""
Rectangle(t::Turtle, width=10.0, height=10.0)
Draw a filled rectangle centered at the current position with the given radius.
"""
function Rectangle(t::Turtle, width=10.0, height=10.0)
gsave()
sethue(t.pencolor...)
rect(t.xpos-width/2, t.ypos-height/2, width, height, :fill)
grestore()
end
"""
Push(t::Turtle)
Save the turtle's position and orientation on a stack.
"""
function Push(t::Turtle)
# save xpos, ypos, turn in a queue
global queue
push!(queue, [t.xpos, t.ypos, t.orientation])
end
"""
Pop(t::Turtle)
Lift the turtle's position and orientation off a stack.
"""
function Pop(t::Turtle)
# restore xpos, ypos, turn from queue
global queue
if isempty(queue)
@info("stack was already empty")
t.xpos, t.ypos, t.orientation = 0.0, 0.0, 0.0
else
t.xpos, t.ypos, t.orientation = pop!(queue)
end
end
"""
Message(t::Turtle, txt)
Write some text at the current position.
"""
function Message(t::Turtle, txt)
gsave()
sethue(t.pencolor...)
text(txt, t.xpos, t.ypos)
grestore()
end
"""
HueShift(t::Turtle, inc=1.0)
Shift the Hue of the turtle's pen forward by `inc`. Hue values range between
0 and 360. (Don't start with black, otherwise the saturation and brightness values
will be black.)
"""
function HueShift(t::Turtle, inc=1.0)
r, g, b = t.pencolor
hsv = convert(Colors.HSV, Colors.RGB(r, g, b))
newhsv = (mod(hsv.h + inc, 360), hsv.s, hsv.v)
newrgb = convert(Colors.RGB, Colors.HSV(newhsv[1], newhsv[2], newhsv[3]))
sethue(newrgb)
t.pencolor = (newrgb.r, newrgb.g, newrgb.b)
end
"""
Randomize_saturation(t::Turtle)
Randomize the saturation of the turtle's pen color.
"""
function Randomize_saturation(t::Turtle)
r, g, b = t.pencolor
hsv = convert(Colors.HSV, Colors.RGB(r, g, b))
newhsv = (hsv.h, rand(0.5:0.05:1.0), hsv.v)
newrgb= convert(Colors.RGB, Colors.HSV(newhsv[1], newhsv[2], newhsv[3]))
sethue(newrgb)
t.pencolor = (newrgb.r, newrgb.g, newrgb.b)
end
"""
Pencolor(t::Turtle, r, g, b)
Set the Red, Green, and Blue colors of the turtle.
"""
function Pencolor(t::Turtle, r, g, b)
sethue(r, g, b)
t.pencolor = (r, g, b)
end
function Pencolor(t::Turtle, col::Colors.Colorant)
temp = convert(RGBA, col)
Pencolor(t::Turtle, temp.r, temp.g, temp.b)
end
function Pencolor(t::Turtle, col::AbstractString)
temp = parse(RGBA, col)
Pencolor(t::Turtle, temp.r, temp.g, temp.b)
end
Pencolor(t::Turtle, col::NTuple{3, Number}) = Pencolor(t, col...)
"""
Reposition(t::Turtle, pos::Point)
Reposition(t::Turtle, x, y)
Reposition: pick the turtle up and place it at another position.
"""
function Reposition(t::Turtle, x, y)
t.xpos = x
t.ypos = y
end
Reposition(t::Turtle, pos::Point) = Reposition(t, pos.x, pos.y)
"""
Penwidth(t::Turtle, w)
Set the width of the line drawn.
"""
Penwidth(t::Turtle, w) = setline(w)
"""
Pen_opacity_random(t::Turtle)
Change the opacity of the pen to some value at random.
"""
Pen_opacity_random(t::Turtle) = setopacity(rand())
# end