Thursday, April 23, 2009

Ruby-Processing

ShoesでのParticleの描画がダメだったので
Ruby-Processingというライブラリを試してみた
JRubyが必要かと思いきや
sudo gem install ruby-processing
だけで実行できた
今度はうまくいった

実行は
rp5 run mysketch.rb
とするとするとProcessingのwindowが立ち上がる

live codingという技もあり 
rb5 live sample.rb
でirbのようなインタラクティブ環境が立ち上がって
インスタンス変数とかをいじれるようになる

以下のコードを見るとShoesがProcessingの影響大であることがわかる
sampleがあるのでdev以下に置いておこう

   1  # My Sketch

   2  class Particle

   3    def initialize(w, h)

   4      @width, @height = w, h

   5      @x, @y, @xvel, @yvel = rand(@width), rand(@height), rand(1)*5-2.5, rand(1)*5-2.5

   6      @r, @g, @b = rand(255), rand(255), rand(255)

   7      @a = 0

   8    end

   9  

  10    def update

  11      return if @a > 2 * 3.14

  12      px, py = @x, @y

  13      @x += @xvel

  14      @y += @yvel

  15      @yvel += 0.1

  16      @xvel *= -1 if (@x < 0 || @x > @width)

  17      @yvel *= -1 if (@y < 0 || @y > @height)

  18      $app.stroke(@r, @g, @b, Math.sin(@a += 0.01)*255)

  19      $app.line px, py, @x, @y

  20    end

  21  end

  22  

  23  class MySketch < Processing::App

  24    def setup

  25      size 400, 400

  26      background 255, 255, 255

  27      @particles = Array.new(20){ Particle.new(width, height) }

  28    end

  29  

  30    def draw

  31      @particles.each { |p| p.update }

  32    end

  33  end

No comments: