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

Thursday 28 May 2015

Making use of ruby-2.0 syntax to test weighted rule selection

def weighted_random(rules)
  total = rules.values.reduce(&:+)
  srand
  chance = rand(0..total)
  rules.each do |item, weight|
    return item unless chance > weight
    chance -= weight
  end
end

symbols = %i(bird fish turtle)        # ruby 2.0
weights = [0.5, 0.4, 0.1]             # for convenience sum = 1.0
rules = symbols.zip(weights).to_h     # ruby 2.0

count_bird = 0
count_fish = 0
count_turtle = 0

10_000.times do
  case weighted_random(rules)
  when :bird
      count_bird += 1.0
  when :fish
      count_fish += 1.0
  when :turtle
      count_turtle += 1.0
  end
end

puts format('bird   %0.4f', count_bird / 10_000)
puts format('fish   %0.4f', count_fish / 10_000)
puts format('turtle %0.4f', count_turtle / 10_000)

Result
bird   0.5001
fish   0.3976
turtle 0.1023

Tuesday 26 May 2015

Alternative development environments for ruby-processing and JRubyArt

NetBeans

No really, netbeans can be used as a development environment for ruby-processing (and might be a sensible choice on windows). The first hurdle is to install the jruby netbeans plugin thereafter things get easier (you can use the plugin to install ruby-processing gem, but you will still need to install vanilla processing, unless using JRubyArt). NB you sometimes have to be a bit patient (ruby-plugin slows down the ide and loading gems etc can take a time).

JEdit

Not really an ide but comes close with the ruby-plugin (but the plugin is in serious need of an update ruby 1.86) see live editing and my jedit resources here for my macro and console plugins

Vim

An editor favoured by many ruby users (combine with tmux etc for true awesomeness). Create a sketch in vim, fire it up with rp5 watch and you have a nice replish way of working.
To simply run the sketch you are currently editing :!rp5 run % or to force use of jruby-complete :!rp5 --nojruby run %

Emacs

Some kind person could produce an Emacs major-mode for ruby-processing, like the processing-mode. Still popular...

Textmate / Sublime

Popular on the Mac see ruby-processing textmate bundle, which also could do with updating, but could get you started.

The Processing Ide

Not an option, Tyfkda did some preliminary work on a ruby-mode for processing, but is difficult.

Atom editor

Looks to be a good editor might be perfect match for ruby-processing development?....

Wednesday 20 May 2015

Bleeding Edge Stuff With Processing-3.0a9

What a JRubyArt sketch might look like with processing-3.0a9, to set title need to set title of surface (which is a protected field), we make this easy by introducing a 'sketch_title' method that does the clever stuff under the hood. Also note size has moved out of setup to settings method (processing does this for you so you don't see it with vanilla processing). Looking good so far with modified 'trefoil' sketch also monjori' shader (latter requires 'k9 run monjori.rb' as expected), all running with jruby-9.0.0.0-pre2. NB without setting a title get some crazy default title like 'org.jruby.proxy.processing.core.PApplet$Proxy1'.
require 'jruby_art'

class Fred < Processing::AppGL
  def setup
    sketch_title 'Fred' # uses surface.setTitle
  end

  def draw
    background 0
    lights
    fill 200, 0, 0
    translate 150, 150
    rotate_x 0.01 * frame_count
    rotate_y 0.02 * frame_count
    box(90, 90, 90)
  end

  def settings
    size 300, 300, P3D
  end
end
# currently can't set sketch title this way use setup method
Fred.new(title: 'Fred')

Wednesday 13 May 2015

Some observations and reflections

It now seem very likely that vanilla processing changes will be very significant (this is a good thing) and such changes will likely take some time to solidify. For that reason there is not much point in ruby-processing (or JRubyArt) trying to keep up. Everything seemed to be pointing toward a LWJGL3 (AndrĂ©s Colubri) future (but now it's changed back to jogl update 16 May 2015), and possible javafx integration (Ben Fry). Seems the LWJGL3 project is too unstable?  So near term efforts will concentrate on jruby-9.0.0.0 and ruby-2.2 syntax changes/opportunities using the existing processing-2.2.1 release.  Possible opportunity for jruby games...?

Sunday 3 May 2015

Adding structure to processing examples (in ruby-processing)

Thinking about ruby-2.0 and structure got me thinking about processing color, but then I though about Struct and OStruct, as easy way to add structure to these processing sketches, see how this gives meaning to fill for example:-
Hue
# Hue is the color reflected from or transmitted through an object
# and is typically referred to as the name of the color (red, blue, yellow,
# etc.). Move the cursor vertically over each bar to alter its hue.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  color_mode HSB, 360
  no_stroke
  @bar_width = 20
  @color_array = (0..width / bar_width).map do
    OpenStruct.new(hue: height, saturation: height, brightness: height)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].hue = mouse_y if range.include?(mouse_x)
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

Saturation
# Saturation is the strength or purity of the color and represents the
# amount of gray in proportion to the hue. A "saturated" color is pure
# and an "unsaturated" color has a large percentage of gray.
# Move the cursor vertically over each bar to alter its saturation.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  color_mode HSB, 360
  no_stroke
  @bar_width = 20
  @color_array = (0..width / bar_width).map do |n|
    OpenStruct.new(hue: n * bar_width, saturation: height, brightness: height / 1.5)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].saturation = mouse_y if range.include? mouse_x
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

Brightness
# Brightness is the relative lightness or darkness of a color.
# Move the cursor vertically over each bar to alter its brightness.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  no_stroke
  color_mode HSB, width, 100, height
  @bar_width = 20
  @color_array = (0..width / bar_width).map do |n|
    OpenStruct.new(hue: n * bar_width, saturation: 100, brightness: height)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].brightness = mouse_y if range.include? mouse_x
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

First class keyword arguments in ruby-processing?

In vanilla processing there are many multi parameter arguments (sometimes overloaded, usual java stuff) that can cause offence to the average rubyist, take camera for example:-
// java/processing 9 float arguments
void camera(
     float eyeX,
     float eyeY,
     float eyeZ,
     float centerX,
     float centerY,
     float centerZ,
     float upX,
     float upY,
     float upZ){...}

// default values
camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);

Since we can now use ruby-2.0 syntax (with jruby-9.0.0.0) there is scope for improvement, by using keyword arguments and Vector object (need not be Vec3D a Struct would do):-
# ruby 3 named parameters (order would be unimportant)
def camera_vec(
               eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)),
               center: Vec3D.new(width / 2.0, height / 2.0, 0),
               up: Vec3D.new(0, 1.0, 0))
  camera(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z)
end

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