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

Thursday, 11 February 2010

A Pentive? Fractal

The pentive fractal is another space filling fractal that I found over on a fractint site again for the grammar generator see my Cesàro fractal.

########################################################
# A Pentive fractal implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
########################################################
require 'pentive'

class Pentive_Test < Processing::App
  attr_reader :pentive,:points, :production

  def setup
    size(600, 400)
    @pentive = Pentive.new(width/95, height*0.9)
    @production = pentive.create_grammar(8)
    @points = pentive.translate_rules(production)
    no_loop()
  end

  def draw()
    background(0)
    stroke(255)
    points.each do |tmp|
      line(*tmp)
    end
  end
end

####################################################
# The Pentive? fractal
####################################################
class Pentive

  attr_reader :draw_length, :xpos, :ypos, :theta, :axiom, :grammar, :delta
  DELTA = Math::PI/180 * 36 # 36 degrees

  def initialize xpos, ypos
    @axiom = "Q"  
    @theta  = -DELTA
    @grammar = Grammar.new(axiom)
    grammar.add_rule("F", "")
    grammar.add_rule("P","1-FR3+FS1-FU")   # abbreviated grammar 1 = two & 3 = four repeats
    grammar.add_rule("Q", "FT1+FR3-FS1+")
    grammar.add_rule("R", "1+FP3-FQ1+FT")
    grammar.add_rule("S", "FU1-FP3+FQ1-")  
    grammar.add_rule("T", "+FU1-FP+")
    grammar.add_rule("U", "-FQ1+FT-")
    @draw_length = 12
    @xpos = xpos
    @ypos = ypos
  end

  def create_grammar(gen)  
    grammar.generate(gen)
  end

  def translate_rules(prod)
    repeats = 1
    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 * repeats
        repeats = 1    
      when "-"
        @theta -= DELTA * repeats
        repeats = 1
      when '1', '3'
        repeats += Integer(ch)
      when "P", "Q", "R", "S", "T", "U"        
      else
        puts("character '#{ch}' not in grammar")
      end
   end
    return points
  end
end




No comments:

Post a Comment

Followers

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