Revised as of 27 December 2009 to use my custom grammar library. See Cesàro fractal in a later post for code.
##
# Lindenmayer System in ruby-processing by Martin Prout
###
require 'dragon'
class Dragon_Test < Processing::App
load_libraries 'grammar'
attr_reader :dragon
def setup
size 600, 480
@dragon = Dragon.new
dragon.create_grammar 10
no_loop
end
def draw
background 0
dragon.render
end
end
############################
# dragon.rb
# Dragon Fractal
###########################
class Dragon
include Processing::Proxy
attr_accessor :axiom, :grammar, :start_length, :theta, :production, :draw_length
def initialize
@axiom = "FX"
@grammar = Grammar.new(axiom)
grammar.add_rule "X", "+FX--FY+" # replace X rule see 'grammar' library
grammar.add_rule "Y", "-FX++FY-" # replace Y rule see 'grammar' library
@start_length = 240.0
@draw_length = start_length
@theta = (Math::PI/180) * 45.0 # convert degrees to radians
@production = axiom
stroke 255
end
def render # using affine transforms
translate(width/3, height/3)
rotate theta * 2
production.each_char do |element|
case element
when 'F'
line(0, 0, 0, -draw_length)
translate(0, -draw_length)
when '+'
rotate theta
when '-'
rotate -theta
when 'X','Y' # do nothing except recognize the grammar
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.64**gen
@production = grammar.generate gen
end
end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Showing posts with label heighway dragon. Show all posts
Showing posts with label heighway dragon. Show all posts
Sunday, 22 November 2009
Sunday, 8 November 2009
Heighway Dragon in ruby-processing context free DSL
This implementation of the Heighway dragon starts with square, that is hidden by being transparent, the rotations scaling and translations used make this solution similar to the paper folding solution.
# dragon.rb ruby-processing NB: :alpha is now implemented in ruby-processing (since 8 Nov 2009) the brightness, of dragon in the start rule has no effect, but the DSL doesn't like to call rules with no arguments.
load_library 'context_free'
def setup_the_dragon
@dragon = ContextFree.define do
rule :start do
dragon :brightness => 1.0
end
rule :dragon do
split do
fractal :hue => 0, :brightness => 1.0, :saturation => 1.0
rewind
fractal :hue => 0.2, :brightness => 1.0, :saturation => 1.0, :rotation => 90, :x => -0.4
rewind
fractal :hue => 0.7, :brightness => 1.0, :saturation => 1.0, :rotation => 270, :x => -0.4, :y => -0.33
rewind
end
end
rule :fractal do
square :alpha => 0.01
split do
fractal :size => 1/Math.sqrt(2), :rotation => -45, :x => 0.25, :y => 0.25
rewind
fractal :size => 1/Math.sqrt(2), :rotation => 135, :x => 0.25, :y => 0.25
rewind
end
end
end
end
def setup
size 800, 800
setup_the_dragon
smooth
draw_it
save_frame("dragon.png")
end
def draw
# Do nothing.
end
def draw_it
background 0
@dragon.render :start, :size => width*0.6, :stop_size => 2,
:start_x => width*0.6, :start_y => height*0.6
end
# dragon.rb ruby-processing NB: :alpha is now implemented in ruby-processing (since 8 Nov 2009) the brightness, of dragon in the start rule has no effect, but the DSL doesn't like to call rules with no arguments.
load_library 'context_free'
def setup_the_dragon
@dragon = ContextFree.define do
rule :start do
dragon :brightness => 1.0
end
rule :dragon do
split do
fractal :hue => 0, :brightness => 1.0, :saturation => 1.0
rewind
fractal :hue => 0.2, :brightness => 1.0, :saturation => 1.0, :rotation => 90, :x => -0.4
rewind
fractal :hue => 0.7, :brightness => 1.0, :saturation => 1.0, :rotation => 270, :x => -0.4, :y => -0.33
rewind
end
end
rule :fractal do
square :alpha => 0.01
split do
fractal :size => 1/Math.sqrt(2), :rotation => -45, :x => 0.25, :y => 0.25
rewind
fractal :size => 1/Math.sqrt(2), :rotation => 135, :x => 0.25, :y => 0.25
rewind
end
end
end
end
def setup
size 800, 800
setup_the_dragon
smooth
draw_it
save_frame("dragon.png")
end
def draw
# Do nothing.
end
def draw_it
background 0
@dragon.render :start, :size => width*0.6, :stop_size => 2,
:start_x => width*0.6, :start_y => height*0.6
end
Labels:
context free,
DSL,
heighway dragon,
ruby processing
Subscribe to:
Posts (Atom)
Followers
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