class Lsystem

Attributes

dir[RW]
putout[RW]

Public Class Methods

new(width, height, title = "L-system") click to toggle source
# File lib/kaki/lsystem.rb, line 5
def initialize(width, height, title = "L-system")
  @width  = width
  @height = height
  @title = title
  @procedure = ""
  @command = {}
  @rule = {}
  @eval = proc {}
  @po = [0, 0]
  @putout = false
end

Public Instance Methods

draw(n) click to toggle source
# File lib/kaki/lsystem.rb, line 65
def draw(n)
  ob = self
  Oekaki.app width: @width, height: @height, title: @title do
    t = Oekaki::Turtle.new
    ob.generate(n)
    
    draw do
      clear
      t.instance_exec(&ob.instance_variable_get(:@eval))
      t.move(*ob.instance_variable_get(:@po))
      t.dir = ob.dir if ob.dir
      ob.exec(t)
    end
  end
end
exec(t) click to toggle source
# File lib/kaki/lsystem.rb, line 49
def exec(t)
  stack = []
  @procedure.each_char do |c|
    case c
    when "["
      stack << t.dup
    when "]"
      t = stack.pop
    else
      todo = @command[c]
      raise "#{c} not defined." unless todo
      t.instance_exec(&todo)
    end
  end
end
generate(n) click to toggle source
# File lib/kaki/lsystem.rb, line 38
def generate(n)
  n.times do
    pr = ""
    @procedure.each_char do |c|
      pr += @rule[c] || c
    end
    @procedure = pr
    p pr if @putout
  end
end
init(st) click to toggle source
# File lib/kaki/lsystem.rb, line 22
def init(st)
  @procedure = st
end
move(x, y) click to toggle source
# File lib/kaki/lsystem.rb, line 18
def move(x, y)
  @po = [x, y]
end
prologue(&block) click to toggle source
# File lib/kaki/lsystem.rb, line 34
def prologue(&block)
  @eval = block
end
rule(a, r) click to toggle source
# File lib/kaki/lsystem.rb, line 30
def rule(a, r)
  @rule[a] = r
end
set(st, &block) click to toggle source
# File lib/kaki/lsystem.rb, line 26
def set(st, &block)
  @command[st] = block
end