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

Wednesday, 11 November 2009

Dynamic rand with ruby-processing context-free DSL

Regular context free can't do dynamic random, here I experiment with the possibilities of ruby-processing DSL. This example is essentially a modified city.rb that is included as a sample with the application. Note I have added a convenience method rand_range for use in the DSL, partly as an experiment in ways of extending the program.


load_library 'context_free'

def setup_the_spots
  @spots = ContextFree.define do
    
    class << self 
      define_method (:rand_range) do |max, min|  # nice to have a range
      rand * (max - min) + min
      end
    end
    
    rule :region do
      split do
        block :x => -0.25, :y => -0.25, :brightness => 1.0, :saturation => 1.0
        rewind
        block :x => 0.25, :y => -0.25, :brightness => 1.0, :saturation => 1.0
        rewind
        block :x => 0.25, :y => 0.25, :brightness => 1.0, :saturation => 1.0
        rewind
        block :x => -0.25, :y => 0.25, :brightness => 1.0, :saturation => 1.0
      end
    end
    
    rule :block do
      spots :size => 0.85, :hue => rand, :saturation => rand_range(1.0, 0.8), :brightness => rand_range(1.0, 0.8)
    end
    
    rule :block, 5 do
      region :size => 0.5, :rotation => rand_range(8, 4), :hue => rand_range(0.8, 0.5), :saturation => rand_range(1.0, 0.5), :brightness => rand_range(1.0, 0.5)
    end
    
    rule :block, 0.1 do
      # Do nothing
    end
    
    rule :spots do
      circle :hue => rand_range(0.8, 0.5), :saturation => 1.0, :brightness => 1.0, :alpha => 0.4
    end
    
  end
end


def setup
  size 600, 600
  background 255, 255, 0
  smooth
  color_mode HSB, 1.0  
  setup_the_spots
  draw_it
end

def draw
  # Do nothing
end

def draw_it
  @spots.render :region, :color => [0.2, 1.0, 1.0, 1.0],
               :start_x => width/2, :start_y => height/2,
               :size => height/2.5
end

def mouse_clicked
  draw_it
end




 

No comments:

Post a Comment

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