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

Thursday, 6 May 2010

Custom hbar shape for ruby processing Context Free DSL

Here I use a custom horizontal bar to mimic a electrophoresis gel (such as PCR). Also demonstrates the use of a low probability, empty rule to terminate recursion (see the third definition of the band rule).

#########################
# electrophoresis.rb
# demonstrates the hbar
# custom terminal
#########################
load_library 'context_free'

def setup_the_gel
  @pcr = ContextFree.define do
    ############ Begin defining custom terminal, a proportional horizontal bar
    class << self   
      define_method(:hbar) do |some_options|
        size, options = *self.get_shape_values(some_options)
        ht = some_options[:ht]|| 0.1    # default hbar width is 0.1
        ratio = ht * size
        rot = options[:rotation]
        rect_mode(CENTER)
        rotate(rot) if rot
        rect(-0.5 * size, -0.5 * ratio, 0.5 * size, 0.5 * ratio)
      end
    end
    ########### End definition of custom terminal 'hbar'
 
    rule :gel do
      dna :brightness => 1.0
    end
 
    rule :dna do
      split do
        26.times do
          band :x => 0.6
          rewind
        end
      end
    end
 
    rule :band do       # narrow band with 0.66' probability        
      hbar :size => 0.8, :ht => 0.1, :brightness => 0.3, :alpha => 0.3, :hue => 0.7, :saturation => 1.0
      band :brightness => 0.5
    end
 
    rule :band, 0.5 do   # double width band with 0.33' probability      
      hbar :size => 0.8, :ht => 0.15, :brightness => 0.8, :alpha => 0.6, :hue => -0.1, :saturation => 1.0
      band :brightness => 0.5
    end
 
    rule :band, 0.08 do  # a low probability empty rule used to end recursion    
    end 
   
   rule :band do            
     band :y => -0.23
   end
 
    rule :band do         
      band :y => 0.17
    end
 
    rule :band do
       band :y => 0.29 
    end

   rule :band do
      band :y => -0.33 
   end

  end
end

def setup
  size 500, 300
  background 0, 0, 180
  smooth
  setup_the_gel
  draw_it
end

def draw_it
  @pcr.render :gel, :start_x => -50, :start_y => height/2,
               :size => height/5, :color => [0.7, 0.8, 0.8, 1.0]
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