ruby-processingでProcessing->Examples->Basic->Arrayの写経

 写経してみたw;

case :ArrayObjects
when :ArrayObjects
  class Mod
    def initialize mx,my,x,y,speed,size
      @mx,@my,@x,@y,@speed = mx,my,x.to_i,y.to_i,speed
      @xdir,@ydir = 1,1
      @size = size
    end
    
    def update
      @x += @speed * @xdir
      if @x >= @size or @x <= 0
        @xdir *= -1
        @x += 1*@xdir
        @y += 1*@ydir
      end
      if @y >= @size or @y <= 0
        @ydir *= -1
        @y += 1*@ydir
      end
      
    end
    
    def draw
      #?
      stroke second*4
      point @mx+@x-1,@my+@y-1
    end
  end
  
  def setup
    size 200,200
    background 176
    no_stroke
    
    @unit = 40
    @num = width/@unit * height/@unit
    @mods = []
    
    (0...(height/@unit)).each{|x|
      (0...(width/@unit)).each{|y|
        @mods << Mod.new(y*@unit, x*@unit, @unit/2, @unit/2, random(0.05, 0.8), @unit)
      }
    }
  end

  def draw
    @mods.each{|mod|
      mod.update
      mod.draw
    }
  end
when :Array
  def setup
    size 200,200
    @ary = []
    
    no_loop
  end

  def draw
    (0...width).each{|y|
      #map input_value,
      #    range_min,        range_max
      #=>  mapped_range_min, mapped_range_max
      @ary << cos(map(y,0, width,0, PI*2)).abs
    }
    @ary.each_with_index{|n,i|
      stroke n*255 # n is (0..1)
      line i,0,i,height/3
      stroke n*255/4
      line i,height/3,i,height/3*2
      stroke 255-n*255
      line i,height/3*2,i,height
    }
      
  end
when :Array2D
  def setup
    size 200,200
    
    background 0
    
    no_loop
  end

  def draw
    #dist src_x, src_y, dest_x,dest_y
    # srcとdest間の距離
    maxdist = dist width/2, height/2, width,height
    
    dist = []
    
    (0...height).each{|x|
      ary = []
      (0...width).each{|y|
        n = dist width/2,height/2,y,x
        ary << n/maxdist * 255
      }
      dist << ary
    }
    
    stroke_weight 1
    
    (0...height).each{|x|
      (0...width).each{|y|
        stroke dist[x][y]
        point y,x
      }
    }
  end
else
end