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

Sunday 29 November 2009

Pentagonal Fractal

Here as as far as I know I have created my own pentagonal fractal, key things are the axiom has 5 go forward turn right elements, to go with a pentagonal structure.
The angle is 72 degrees to go with a pentagonal structure. The reduce factor has been calculated for nesting pentagon structures. There is an interesting diversion in shapes with the different number of repetitions. If you want to run other that the three repetitions you will need to adjust the starting position (and size particularly for the four repetitions).

##
# A Pentagonal Fractal created using a
# Lindenmayer System in ruby-processing by Martin Prout
###
require 'pentagonal'

class Pentagonal_Test < Processing::App
  attr_reader :pentagonal
  def setup
    size 800, 800
    @pentagonal = Pentagonal.new
    pentagonal.simulate 3
    no_loop   
  end
  def draw
    background 0
    pentagonal.render
  end
end

############################
# pentagonal.rb here I roll one of my own
###########################

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

class Pentagonal
  include Processing::Proxy
  SCALE = (3 - Math.sqrt(5))/2   # approximately 0.382
  DELTA = (Math::PI/180) * 72.0  # convert degrees to radians
  attr_accessor :axiom, :rule, :start_length, :theta, :production, :draw_length, :xpos, :ypos

  def initialize
    @axiom = "F-F-F-F-F"
    @rule = "F+F+F-F-F-F+F+F"      # replace F with this string see iterate function
    @start_length = 220
    @theta = 0.0
    @xpos = width*0.75
    @ypos = height*0.85
    stroke 255
    reset
  end

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

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

  end

  def render
    production.each_char do |element|
      case element
      when 'F'                     # you could use affine transforms here, I prefer to do the Math
        line(xpos, ypos, (@xpos -= draw_length * Math.cos(theta)), (@ypos += draw_length * Math.sin(theta)))  
      when '+'
        @theta += DELTA        
      when '-'
        @theta -= DELTA        
      else puts "Grammar not recognized"
      end
    end
  end

  def simulate gen
    gen.times do
      @production = iterate(@production, @rule)
    end
  end
end







two repetitions






three repetitions















four repetitions

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