Class: MathUtils

Inherits:
Object show all
Defined in:
lib/math_utils.rb

Overview

All generic static math functions needed.

Class Method Summary collapse

Class Method Details

.division_fade(detail = 8, factor = nil) ⇒ Object

outputs 0 to 1 increasing by the factor

detail

number of times to apply division. 1 to inf

factor

the number to divide by.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/math_utils.rb', line 12

def self.division_fade(detail = 8, factor = nil)
  factor ||= GR
  raise "Detail must be >= 1" if detail < 1
  out = Array.new(detail)
  old = 1
  out.each_with_index do |x,i| 
    out[i] = old / factor.to_f
    out[i] = 1 if i < 1 # first is 1 always
    old = out[i]
  end
  out.reverse
end

.fib(n) ⇒ Object

calculate fibonacci number



62
63
64
# File 'lib/math_utils.rb', line 62

def self.fib(n)
  (FIB_MATRIX**(n-1))[0,0]
end

.filloutwave(data) ⇒ Object

generates wave cycle by duplicating data to fillout the other 3 sections of a wave.

data

data from 0 to 1 for the first section.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/math_utils.rb', line 41

def self.filloutwave(data)
  val = []
  i=0
  while i < data.count
    val.push data[data.count-i-1] if i > 0
    i+=1
  end
  ret = data + val
  val = []
  i=0
  while i < ret.count
    val.push -ret[ret.count-1-i] if i > 0
    i+=1
  end
  ret += val
  ret.pop # remove last 0
  ret
end

.sinwave(detail = 2048, saturation = 0) ⇒ Object

generates data for sin wave in an array (single cycle)

detail

number of elements in the wave.

default: 2048 very smooth



28
29
30
31
32
33
34
35
36
37
# File 'lib/math_utils.rb', line 28

def self.sinwave(detail = 2048, saturation=0)
  raise "Sinwave frames must be specifed as >= 3." if detail < 3
  val = Array.new(detail)
  val.each_with_index do |foo,i|
    progress=i.to_f/detail
    val[i] = Math.sin(2.0*Math::PI*progress)
    val[i] = (val[i]-saturation.to_f)+rand*saturation.to_f*2.0
  end
  val
end