jashkenas edited this page Sep 13, 2010 · 9 revisions

context_free

… is a library for Ruby-Processing that allows you to draw context-free sketches using a small declarative DSL. You can define rules which call one another probabilistically. The following block of code is the complete definition for drawing a fractal fern [samples/fern.rb] :

@fern = ContextFree.define do

  rule :start do
    fern :rotation => 50, :hue => 0.8
  end

  rule :fern do
    circle :size => 0.75, :rotation => -10
    split do
      fern :size => 0.92, :y => -2, :rotation => -5, :hue => 0.85
      rewind
      fern :size => 0.5, :y => -2, :rotation => 90
      rewind                                  
      fern :size => 0.5, :y => -2, :rotation => -90
    end
  end

end

With a little processing glue, produces:


There are several other samples — tree.rb, city.rb, and vine.rb — if you have one to contribute, please do.



Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.