For the grammar.rb library see my Cesàro fractal (here it was included in the davidtour.rb file which is why it didn't need to be separately loaded, Grammar code omitted for brevity).
########################################################
# A David Tour fractal implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
########################################################
require 'davidtour'
class David_Test < Processing::App
attr_reader :david, :points, :production
def setup
size(800, 900)
@david = DavidTour.new(width * 0.6, height/4)
@production = david.create_grammar(5)
@points = david.translate_rules(production)
no_loop()
end
def draw()
background(0)
stroke(255)
points.each do |tmp|
line(*tmp)
end
end
end
####################################################
# The DavidTour fractal has been used to study the
# Euclidean travelling salesmam problem
####################################################
class DavidTour
attr_reader :draw_length, :xpos, :ypos, :theta, :axiom, :grammar
DELTA = Math::PI/3 # 60 degrees
def initialize xpos, ypos
@axiom = "FX-XFX-XFX-XFX-XFX-XF"
@theta = 0
@grammar = Grammar.new(axiom)
grammar.add_rule("F", "!F!-F-!F!")
grammar.add_rule("X", "!X")
@draw_length = 15
@xpos = xpos
@ypos = ypos
end
def create_grammar(gen)
@draw_length *= @draw_length * 0.5**gen
grammar.generate(gen)
end
def translate_rules(prod)
swap = false
points = [] # An empty array to store lines as an array of points
prod.scan(/./) do |ch|
case(ch)
when 'F'
temp = [xpos, ypos, (@xpos += draw_length * Math.cos(theta)), (@ypos -= draw_length * Math.sin(theta))]
points.push(temp)
when '+'
@theta += (DELTA)
when '-'
@theta += (swap ? DELTA : -DELTA)
when '!'
swap = !swap
when 'X'
else
puts("character '#{ch}' not in grammar")
end
end
return points
end
end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Wednesday, 10 February 2010
Subscribe to:
Post Comments (Atom)
Followers
About Me
- monkstone
- I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2
No comments:
Post a Comment