Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Wednesday, 5 May 2010
Penrose Tiling Ruby-Processing LSystems
######################################################
# A Lindenmayer System in ruby-processing by Martin Prout
# Loosely based on a processing PenroseTiling sketch
# by Geraldine Sarmiento
# tiling.rb
######################################################
require 'penrose_tiling'
class Penrose < Processing::App
load_libraries "grammar"
attr_reader :penrose
def setup
size 1000, 1000
stroke 255, 60
stroke_weight 2
smooth
@penrose = PenroseTiling.new
penrose.create_grammar 4
no_loop
end
def draw
background 0
penrose.render
end
end
########################################
# penrose_tiling.rb
########################################
class PenroseTiling
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 = PI/5 # radians or 36 degrees
def initialize
@axiom = "[X]2+[X]2+[X]2+[X]2+[X]" # Note use of abbreviated rule
@grammar = Grammar.new axiom # here number equals number of repeats
@grammar.add_rule "F", ""
@grammar.add_rule "X", "+YF2-ZF[3-WF2-XF]+"
@grammar.add_rule "Y", "-WF2+XF[3+YF2+ZF]-"
@grammar.add_rule "Z", "2-YF4+WF[+ZF4+XF]2-XF"
@grammar.add_rule "W", "YF2+ZF4-XF[-YF4-WF]2+"
@start_length = 1000.0
@theta = 0
@xpos = width/2
@ypos = height/2
@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] # a simple array for turtle
stack = [] # a simple array for stack
production.scan(/./).each do |element|
case element
when 'F'
turtle = draw_line(turtle, draw_length)
when '+'
turtle[ANGLE] += DELTA * repeats
repeats = 1
when '-'
turtle[ANGLE] -= DELTA * repeats
repeats = 1
when '['
stack.push(turtle.dup) # push a copy current turtle to stack
when ']'
turtle = stack.pop # assign current turtle a instance popped from the stack
when 'W', 'X', 'Y', 'Z'
when '2', '3', '4'
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.5**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)
new_xpos = turtle[XPOS] - length * cos(turtle[ANGLE])
new_ypos = turtle[YPOS] - length * sin(turtle[ANGLE])
line(turtle[XPOS], turtle[YPOS], new_xpos, new_ypos)
return [new_xpos, new_ypos, turtle[ANGLE]]
end
end
#############################################################
# library/grammar.rb
# Non-stochastic grammar
# with unique premise/rules
############################################################
class Grammar
attr_accessor :axiom, :rules
def initialize axiom
@axiom = axiom
@rules = Hash.new
end
def add_rule premise, rule
rules.store(premise, rule)
end
##########################################
# replace each pre char with a unique rule
##########################################
def new_production production
production.gsub!(/./) { |c| (r = @rules[c]) ? r : c }
end
##########################################
# control the number of iterations
# default 0, returns the axiom
##########################################
def generate repeat = 0
prod = axiom
repeat.times do
prod = new_production prod
end
return prod
end
end
Labels:
l-system fractal,
penrose tiling,
ruby processing
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
-
▼
2010
(73)
-
▼
May
(9)
- Towards Penrose Tiling Using ruby-processing conte...
- Ruby Processing Context Free DSL (how to do a cfdg...
- Colored Penrose Tiling using LSystems
- Op Art using Voronoi Cell, Ruby Processing
- Voronoi Cells from the Coordinates of A Penrose Ti...
- Random Voronoi Sketch
- A Simple flower using ruby processing context free...
- Custom hbar shape for ruby processing Context Free...
- Penrose Tiling Ruby-Processing LSystems
-
▼
May
(9)
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