-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSystem.sf
53 lines (46 loc) · 1.13 KB
/
LSystem.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
#!/usr/bin/ruby
include('Turtle.sf')
class LSystem(
angle = 90,
scale = 1,
xoff = 0,
yoff = 0,
len = 5,
color = 'black',
width = 500,
height = 500,
turn = 0,
) {
method execute(string, repetitions, filename, rules) {
var theta = angle.deg2rad
var turtle = Turtle(
x: width,
y: height,
angle: turn,
scale: scale,
color: color,
xoff: xoff,
yoff: yoff,
)
var stack = []
var table = Hash(
'+' => { turtle.turn(theta) },
'-' => { turtle.turn(-theta) },
':' => { turtle.mirror },
'[' => { stack.push(turtle.state) },
']' => { turtle.setstate(stack.pop) },
)
repetitions.times {
string.gsub!(/(.)/, {|c| rules{c} \\ c })
}
string.each_char { |c|
if (table.contains(c)) {
table{c}.run
}
elsif (c.is_uppercase) {
turtle.forward(len)
}
}
turtle.save_as(filename)
}
}