Sha256: 83d1851df80d6f2c69c89aad90451558e4325ff4d0999be1885770b4b36af192

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'rubyonacid/factory'

module RubyOnAcid

#Produces a Lissajous curve over pairs of keys.
class LissajousFactory < Factory
  
  #Value to scale Y values by.  Affects the number of loops in a curve.
  attr_accessor :scale

  #Counters used to calculate sine/cosine values will be incremented by this amount with each query.
  attr_accessor :interval
  
  #Takes a hash with all keys supported by Factory, plus these keys and defaults:
  #  :scale => 0.2
  #  :interval => 0.1
  def initialize(options = {})
    super
    @scale = options[:scale] || 0.2
    @interval = options[:interval] || 0.1
    @counters = {}
    @x_y_assignments = {}
    @next_key_assignment = :x
  end
  
  #Assigns the given key to receive either X or Y values from the Lissajous curve, if it isn't already assigned.
  #Then returns the appropriate value for the key.
  def get_unit(key)
    unless @x_y_assignments[key]
      @x_y_assignments[key] = @next_key_assignment
      if @next_key_assignment == :x
        @next_key_assignment = :y
      else
        @next_key_assignment = :x
      end
    end
    @counters[key] ||= 0
    @counters[key] += @interval
    if @x_y_assignments[key] == :x
      return calculate_x(@counters[key])
    else
      return calculate_y(@counters[key])
    end
  end
  
  private
  
    def calculate_x(value)
      (Math.sin(value) + 1) / 2
    end
    
    def calculate_y(value)
      (Math.cos(value * scale) + 1) / 2
    end
  
end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyonacid-0.4.0 lib/rubyonacid/factories/lissajous.rb