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

Wednesday 19 August 2009

Introducing weighted rules (and randomness)


load_library 'context_free'

def setup_the_creature
  @creature = ContextFree.define do
    
    rule :start do
      split do
        11.times do |count|       # 11 times increment rotation by 30 degrees
          legs :rotation => count * 30
          rewind                  # rewind context
        end
        legs :rotation => 360
      end
    end
    
    rule :legs do
      circle :hue => 0.15, :saturation => 0.5, :brightness => 1.0, :color => [0.95, 0.15]
      legs :y => 0.1, :size => 0.965
    end
    
    rule :legs, 0.01 do
      circle
      split do
        legs :rotation => 3 
        rewind
        legs :rotation => -3
      end
    end
    
  end
end

def setup
  size 600, 600
  setup_the_creature
  no_stroke
  color_mode HSB, 1.0
  smooth
  draw_it
  # save_frame("creature.png")
end

def draw
  # Do nothing.
end

def draw_it
  background -1.0
  @creature.render :start, :size => height/5, :stop_size => 0.8,
  :start_x => width/2, :start_y => height/3,  :color => [0.75, 0.1, 0.9]
end

def mouse_clicked
  draw_it
end



Spooky result (makes your processor do a bit of work before it displays)

2 comments:

  1. These are really nice, Martin. You should consider forking the context-free library on GitHub and adding some of these as examples.

    One useful point: At the beginning of your definition, you have a long list of repeated "arms, rewind" rules. It's nice for clarity to lay it all out like that, but, because this is Ruby, and you're simply using a DSL, you can stick a loop (or any piece of arbitrary code) in the middle of it. Say, a count up from 0 to 360 in steps of 30, drawing and rewinding with every step.

    Cheers,
    Jeremy

    ReplyDelete
  2. I was just about to update as suggested when I read your comment as suggested.

    ReplyDelete

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