A Sierpinski (triangle) variant after Mark Meyers nodebox l-systems...
for the grammar library see a previous post Cesàro fractal
##
# A Sierpinski Variant implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
###
require 'sierpinski'
class Sierpinski_Test < Processing::App
load_libraries 'grammar'
attr_reader :sierpinski
def setup
size 500, 600
@sierpinski = Sierpinski.new
sierpinski.create_grammar 8
no_loop
end
def draw
background 0
sierpinski.render
end
end
############################
# sierpinski.rb
###########################
class Sierpinski
include Processing::Proxy
attr_accessor :axiom, :grammar, :start_length, :theta, :production, :draw_length, :xpos, :ypos
DELTA = (Math::PI/180) * 60.0 # convert degrees to radians
GAMMA = (Math::PI/180) * 60.25
def initialize
@axiom = "A"
@grammar = Grammar.new axiom
grammar.add_rule "A", "B+A+B"
grammar.add_rule "B", "A-B-A"
@start_length = 450
@theta = 0.0
@xpos = width * 0.95
@ypos = height * 0.8
stroke 255
@production = axiom
@draw_length = start_length
end
def render # NB not using affine transforms here
repeats = 1
production.each_char do |element|
case element
when 'B', 'A'
line(xpos, ypos, (@xpos -= draw_length * Math.cos(theta)), (@ypos += draw_length * Math.sin(theta)))
when '+'
@theta += DELTA * repeats
repeats = 1
when '-'
@theta -= GAMMA * repeats
repeats = 1
when '2'
repeats = 2
else
puts "Character '#{element}' is 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
end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
-
▼
2010
(73)
-
▼
January
(14)
- Hello Peasy in ruby processing (a simple PeasyCam ...
- Using the PeasyCam library in ruby-processing
- 3D Hilbert Using LSystems and ruby-processing
- Two dimensional Hilbert fractal with LSystems and ...
- Fern Fractal Using LSystems (includes a subtle col...
- Using a symbolic grammar for LSystems ruby-processing
- Islands in ruby processing using my grammar library
- Refactoring my Penrose Snowflake
- A 3D Plant using L-Systems and ruby-processing
- Another control panel example with odd turtle turns
- Adding some controls to Sierpinski sketch
- A Sierpinski Variant using L-Systems and ruby proc...
- An interactive l-system sketch using the Control P...
- Checking the probabilities
-
▼
January
(14)
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