Sha256: 723685a7240277a23bfb3fe572206f3a7898edb5b5f6f5e26c11945b15249979

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

require 'ruby-processing'

# A Rotater class
class Rotater

  def initialize(x, y, speed, width)
    @x, @y = x, y
    # Angle is always initialized to 0
    @theta = 0 
    @speed = speed
    @w = width
  end

  # Increment angle
  def spin
    @theta += @speed
  end

  # Display rectangle
  def display
    # push_matrix and pop_matrix are called inside the class' display method. 
    # This way, every Rotater object is rendered with its own independent translation and rotation!
    $app.push_matrix  
    $app.translate @x, @y
    $app.rotate @theta
    $app.rect 0, 0, @w, @w
    $app.pop_matrix 
  end

end

class RotatingManyThingsSketch < Processing::App

  def setup
    rect_mode CENTER
    stroke 0
    fill 0, 100
    @rotaters = Array.new(20) do
      Rotater.new(rand(width), rand(height), random(-0.1, 0.1), rand(48))
    end
  end

  def draw
    background 255

    # All Rotaters spin and are displayed
    @rotaters.each do |r|
      r.spin
      r.display
    end
  end

end

RotatingManyThingsSketch.new :title => "Rotating Many Things", :width => 200, :height => 200

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-processing-1.0.1 samples/learning_processing/chapter_14/15_rotating_many_things.rb
ruby-processing-1.0.2 samples/learning_processing/chapter_14/15_rotating_many_things.rb
ruby-processing-1.0.4 samples/learning_processing/chapter_14/15_rotating_many_things.rb
ruby-processing-1.0.3 samples/learning_processing/chapter_14/15_rotating_many_things.rb