Class: MTK::Patterns::Lines

Inherits:
Pattern
  • Object
show all
Defined in:
lib/mtk/patterns/lines.rb

Overview

A piecewise linear function (see src="http://en.wikipedia.org/wiki/File:PiecewiseLinear.png" />) defined in terms of [value, steps_to_reach_value] pairs.

The “steps_to_reach_value” for the first element is ignored and may be omitted, since it takes 0 steps to start.

Instance Attribute Summary

Attributes inherited from Pattern

#cycle_count, #element_count, #elements, #max_cycles, #max_elements, #min_elements, #options

Instance Method Summary collapse

Methods inherited from Pattern

#empty?, from_a, #initialize, #max_cycles_exceeded?, #max_elements_exceeded?, #min_elements_unmet?, #next, #rewind

Methods included from Groups::Collection

#==, #[], #clone, #concat, #each, #empty?, #enumerable_map, #first, #last, #map, #partition, #permute, #repeat, #reverse, #rotate, #size, #to_a

Constructor Details

This class inherits a constructor from MTK::Patterns::Pattern

Instance Method Details

#advanceObject (protected)

Update internal state (index, etc) and set @current to the next element.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mtk/patterns/lines.rb', line 23

def advance
  while @step_count >= @steps
    @step_count = 0

    @index += 1
    raise StopIteration if @index >= @elements.length

    @prev = @next
    next_elem = @elements[@index]
    if next_elem.is_a? Array
      @next = next_elem.first
      @steps = next_elem.last.to_f
    else
      @next = next_elem
      @steps = 1.0
    end
  end

  @step_count += 1

  if @prev and @next
    # linear interpolation
    @current = @prev + (@next - @prev)*@step_count/@steps
  else
    @current = @next
  end
end

#rewind_or_cycle(is_cycling = false) ⇒ Object (protected)

Reset the pattern to the beginning

Parameters:

  • is_cycling (Boolean) (defaults to: false)

    true when #next is performing a cycle back to the beginning of the Pattern. false for a normal #rewind



14
15
16
17
18
19
20
# File 'lib/mtk/patterns/lines.rb', line 14

def rewind_or_cycle(is_cycling=false)
  @steps = -1
  @step_count = -1
  @prev = nil
  @next = nil
  super
end