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.



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

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



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

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.



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

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



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

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