-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtle.sf
60 lines (48 loc) · 1.16 KB
/
Turtle.sf
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
#!/usr/bin/ruby
require('Image::Magick')
class Turtle(
x = 500,
y = 500,
angle = 0,
scale = 1,
mirror = 1,
xoff = 0,
yoff = 0,
color = 'black',
) {
has im = %O<Image::Magick>.new(size => "#{x}x#{y}")
method init {
angle.deg2rad!
im.ReadImage('canvas:white')
}
method forward(r) {
var (newx, newy) = (x + r*sin(angle), y + r*-cos(angle))
im.Draw(
primitive => 'line',
points => join(' ',
round(x * scale + xoff),
round(y * scale + yoff),
round(newx * scale + xoff),
round(newy * scale + yoff),
),
stroke => color,
strokewidth => 1,
)
(x, y) = (newx, newy)
}
method save_as(filename) {
im.Write(filename)
}
method turn(theta) {
angle += theta*mirror
}
method state {
[x, y, angle, mirror]
}
method setstate(state) {
(x, y, angle, mirror) = state...
}
method mirror {
mirror.neg!
}
}