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

Sunday 22 November 2009

Penrose Snowflake L-Systems using ruby-processing

Revised as of 15 January, now uses a custom grammar library (see Cesàro fractal in a later post).  If you landed here from http://devver.net/blog/2009/12/playing-with-processing-making-snow/
this is not the code Dan re-factored, and I have come up with some refactoring of my own which I hope is clearer than my original code.

##
# Lindenmayer System in ruby-processing by Martin Prout
# Loosely based on a processing Penrose L-System
# by Geraldine Sarmiento
###

require 'penrose_snowflake'

class Penrose < Processing::App 
  load_libraries "grammar"
  
  attr_reader :penrose
  
  def setup
    size 1000, 900
    stroke 255
    smooth
    @penrose = PenroseSnowflake.new
    penrose.create_grammar 4
    no_loop    
  end
  
  def draw
    background 0
    penrose.render
  end
end

###################################
# penrose_snowflake.rb
###################################
class PenroseSnowflake
  include Processing::Proxy

  attr_reader :axiom, :grammar, :start_length, :theta, :production, :draw_length,
    :repeats, :xpos, :ypos
    
  XPOS = 0     # placeholders for turtle array
  YPOS = 1
  ANGLE = 2  
  DELTA = (Math::PI/180) * 18.0 # convert degrees to radians

  def initialize
    @axiom = "F3-F3-F3-F3-F"
    @grammar = Grammar.new axiom
    grammar.add_rule "F", "F3-F3-F45-F++F3-F"
    @start_length = 450.0
    @theta = 0
    @xpos = width * 0.8
    @ypos = height * 0.95
    @production = axiom
    @draw_length = start_length
  end

  ##############################################################################
  # Not strictly in the spirit of either processing in my render
  # function I have ignored the processing translate/rotate functions in favour
  # of the direct calculation of the new x and y positions, thus avoiding such
  # affine transformations.
  ##############################################################################
  
  def render()
    repeats = 1
    turtle = [xpos, ypos, theta]                # simple array for turtle
    production.scan(/./).each do |element|
      case element
      when 'F'
        turtle = draw_line(turtle, draw_length, repeats)
        repeats = 1
      when '+'
        turtle[ANGLE] += DELTA * repeats
        repeats = 1
      when '-'
        turtle[ANGLE] -= DELTA * repeats
        repeats = 1
      when '3', '4', '5'
        repeats += Integer(element)
      else puts "Character '#{element}' not in grammar"  
      end
    end
  end
  
  ##############################
  # create grammar from axiom and
  # rules (adjust scale)
  ##############################
  
  def create_grammar(gen)
    @draw_length *= 0.4**gen
    @production = grammar.generate gen
  end 

  private
  ######################################################
  # draws line using current turtle and length parameters
  # returns a turtle corresponding to the new position
  ######################################################

  def draw_line(turtle, length, repeats = 1)
    new_xpos = turtle[XPOS] - repeats * length * Math.cos(turtle[ANGLE])
    new_ypos = turtle[YPOS] + repeats * length * Math.sin(turtle[ANGLE])
    line(turtle[XPOS], turtle[YPOS], new_xpos, new_ypos)
    turtle = [new_xpos, new_ypos, turtle[ANGLE]]
  end 
end



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