Class: TexPlay::LSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/texplay-contrib.rb

Overview

L-System code adding LSystem class to TexPlay module

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ LSystem

Returns a new instance of LSystem.



63
64
65
66
67
# File 'lib/texplay-contrib.rb', line 63

def initialize(&block)
  @rules = {}
  
  instance_eval(&block) if block
end

Instance Method Details

#angle(new_angle = nil) ⇒ Object



77
78
79
80
# File 'lib/texplay-contrib.rb', line 77

def angle(new_angle=nil)
  return @angle if !new_angle
  @angle = new_angle
end

#atom(new_atom) ⇒ Object



73
74
75
# File 'lib/texplay-contrib.rb', line 73

def atom(new_atom)
  @atom = new_atom
end

#produce_string(order) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/texplay-contrib.rb', line 82

def produce_string(order)
  order = order[:order]
  string = @atom.dup
  
  order.times do
    i = 0
    while(i < string.length)
      sub = @rules[string[i, 1]]
      
      string[i] = sub if sub
      
      i += sub ? sub.length : 1
    end
  end
  
  string
end

#rule(new_rule) ⇒ Object



69
70
71
# File 'lib/texplay-contrib.rb', line 69

def rule(new_rule)
  @rules.merge!(new_rule)
end