-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl_system.rb
More file actions
68 lines (55 loc) · 1.28 KB
/
l_system.rb
File metadata and controls
68 lines (55 loc) · 1.28 KB
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
require 'ruby-processing'
class LSystem
include Processing::Proxy
attr_reader :axiom
def initialize(start, grammar, rules, length)
@axiom = [start]
@grammar = grammar
@rules = rules
@length = length
end
def generate
new_axiom = []
@axiom.each do |letter|
if @grammar[letter]
@grammar[letter].each_char { |l| new_axiom << l }
else
new_axiom << letter
end
end
@axiom = new_axiom
@length /= 2
end
def draw
@axiom.each do |letter|
@rules[letter].call(@length)
end
end
end
class SystemSketch < Processing::App
def setup
size(400, 400)
# rules = {}
# rules['F'] = "FGF"
# rules['G'] = "GGG"
gramar = { 'F' => 'FF+[+F-F-F]-[-F+F+F]' }
rules = {
'F' => ->(length){ line(0, 0, 0, length); translate(0, length) },
'G' => ->(length){ translate(0, length) },
'+' => ->(radians){ rotate(radians(25)) },
'-' => ->(radians){ rotate(-radians(25)) },
'[' => ->(m=nil){ push_matrix },
']' => ->(m=nil){ pop_matrix }
}
@lsystem = LSystem.new('F', gramar, rules, width/4 )
end
def mouse_pressed
@lsystem.generate
end
def draw
background(255)
translate(width/2, height)
rotate(radians(180))
@lsystem.draw
end
end