Thursday, 19 November 2009
Lindenmayer system in ruby-processing
##
# Lindenmayer System in ruby-processing by Martin Prout
# Loosely based on processing Pentigree L-System by Geraldine Sarmiento
###
require 'pentigree'
class Pentigree_Test < Processing::App
load_library :grammar
attr_reader :pentigree
def setup
size 800, 800
@pentigree = Pentigree.new
pentigree.create_grammar(5)
no_loop
end
def draw
background 0
pentigree.render
end
end
######################
# pentigree.rb
######################
class Pentigree
include Processing::Proxy
attr_reader :axiom, :grammar, :start_length, :theta, :production, :generations, :draw_length
def initialize
@axiom = "F-F-F-F-F"
@grammar = Grammar.new(axiom)
grammar.add_rule("F", "F-F++F+F-F-F")
@start_length = 40.0
@theta = (Math::PI/180) * 72.0 # convert degrees to radians
@production = axiom
@draw_length = start_length
end
def render
translate(width * 0.8, height * 0.75)
@production.each_char do |prod|
case prod
when 'F'
no_fill
stroke 255
line(0, 0, 0, draw_length)
translate(0, draw_length)
when '+'
rotate(theta)
when '-'
rotate(-theta)
when '['
push_matrix()
when ']'
pop_matrix()
else
puts "Character '#{element}' is not in grammar"
end
end
end
def create_grammar(gen)
@draw_length *= 0.6**gen
@production = grammar.generate gen
end
end
No comments:
Post a Comment