Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Sunday, 29 November 2009

Seaweed using Lindenmayer system on ruby processing

Note that the rules here are essentially the same as the simple plant in my previous post, but here I've reduced the basic angle and added some numbers (repeat factors) to the 'rule'.


########################################################
# A simple seaweed implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
########################################################

require 'wrack'

class Wrack_Test < Processing::App
  attr_reader :wrack
  def setup
    size 600, 600
    @wrack = Wrack.new   
    wrack.simulate 4   
    no_loop
  end
  def draw
    background 0
    translate 200, 550
    wrack.render
    save_frame "wrack_4.png"
  end
end

############################
# wrack.rb
###########################

require 'jcode'  ## required until jruby supports ruby 1.9

class Wrack
  include Processing::Proxy
  DELTA = (Math::PI/180) * 18
 
  attr_accessor :axiom, :rule, :rule1, :start_length, :theta, :production, :draw_length, :xpos, :ypos

  def initialize
    @axiom = "F"
    @rule = "3F[2-F]2F[+F][F]"
    @start_length = 90
    stroke 255
    stroke_weight 4
    reset
  end

  def reset                         # initialize or reset variables
    @production = axiom
    @draw_length = start_length
  end

  def iterate prod_, rule_
    @draw_length *=  0.5
    new_production = prod_       
    new_production.gsub!('F', rule_)
  end

  def render
    repeats = 1
    production.each_char do |element|
      case element
      when 'F'                     # NB using affine transforms
        line(0, 0, 0, -draw_length * repeats)
        translate(0, -draw_length * repeats)
        repeats = 1
      when '+'
        rotate(+DELTA * repeats)     
        repeats = 1
      when '-'
        rotate(-DELTA * repeats)       
        repeats = 1
      when '['
        push_matrix
      when ']'
        pop_matrix
      when '1', '2', '3', '4'  # extend this to '9' if you need to
        repeats = Integer(element)
      else puts "Grammar not recognized"
      end
    end
  end
 
  def simulate gen
    gen.times do
      @production = iterate(production, rule)
    end
  end
end


four iterations



five iterations (might look better green?)
 

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2