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

Monday, 4 February 2013

Sand Traveler Refactored and Updated

Just checking that this classic ruby processing example will run with my updated ruby-processing. The main change is that the sketch is now a bare sketch. Other changes I've made include the use of the Processing::Proxy mixin and avoiding bare calls to global $app, and using the ruby 1.9 feature rand range. I've also made use of displayWidth and displayHeight variables (NB: these must be camel case currently) to display full size on screen. I would recommend letting the sketch "grow" a bit before messing with controls to get best effect, see those travellers move....
# Sand Traveler was a special commision, produced in Processing by 
# Jared Tarbell for Sonar 2004, Barcelona.
# This Ruby port has been tweaked, half of the (non-running) code exorcised, 
# color palette broadened, and done in negative.
# refactored and updated for processing-2.0

# -- omygawshkenas

attr_reader :cities, :num, :slowdown, :num_travelers, :panel

load_library 'control_panel'
def setup
  size displayWidth, displayHeight, P2D
  control_panel do |c|
    c.slider :slowdown, 0.5..1.1
    c.slider :num_travelers, 0..50
    @panel = c
  end
  @dx, @dy = 0, 0
  @num = 150
  @cities = []
  @slowdown = 0.954
  @num_travelers = 13
  reset_all
end
def draw
  panel.visible = self.visible
  cities.each {|city| city.move }
end
def mouse_pressed
  reset_all
end
def reset_all
  @source_colors = [[@red || rand(255), @green || rand(255), @blue || rand(255), rand(45 .. 75)],
  [@red_2 || rand(255), @green_2 || rand(255), @blue_2 || rand(255), rand(45 .. 75)]]
  background 0
  vt = 4.2
  vvt = 0.2
  ot = rand * PI * 2
  num.times do |i|
    tinc = ot + (1.1 - i / num) * 2 * i * (PI * 2) / num
    vx, vy = vt * sin(tinc), vt * cos(tinc)
    cities << City.new(self, width/2+vx*2, height/2+vy*2, vx, vy, i)
    vvt -= 0.00033
    vt += vvt
  end
  cities.each { |city| city.find_friend }
end
def some_color
  choice = @source_colors.sample.dup
  choice.map! { |c| c + rand(-60 .. 60) }
  choice[3] = 0.7 * choice[3]
  return choice
end
class City
  include Processing::Proxy
  attr_reader :x, :y, :color, :app, :friend, :vx, :vy
  # SLOWDOWN = 0.946
  def initialize(app, dx, dy, vx, vy, index)
    @app = app
    @x, @y = dx, dy
    @vx, @vy = vx, vy
    @index = index
    @slowdown = 0.956
    @color = app.some_color
  end
  def move
    if (vx.abs + vy.abs) > 0.01
      @vx += (friend.x - x) / 1000
      @vy += (friend.y - y) / 1000
      @vx *= app.slowdown; @vy *= app.slowdown
      @x += vx; @y += vy
      draw_travelers
    end
  end
  def find_friend
    @friend = app.cities[(@index + rand(app.num/5) + 1) % app.num]
  end
  def draw_travelers
    stroke *friend.color
    app.num_travelers.to_i.times do |i|
      t = rand * PI * 2
      dx = sin(t) * (x - friend.x) / 2 + (x + friend.x) / 2
      dy = sin(t) * (y - friend.y) / 2 + (y + friend.y) / 2
      if rand < 0.01
        dx += rand(-3.0 .. 3)
        dy += rand(-3.0 .. 3)
      end
      point dx, dy
    end
  end
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