module Mittsu::Math

Constants

DEGREE_TO_RADIANS_FACTOR
RADIANS_TO_DEGREES_FACTOR

Public Class Methods

clamp(x, a, b) click to toggle source
# File lib/mittsu/math.rb, line 30
def self.clamp(x, a, b)
  ( x < a ) ? a : ( ( x > b ) ? b : x )
end
clamp_bottom(x, a) click to toggle source
# File lib/mittsu/math.rb, line 34
def self.clamp_bottom(x, a)
  x < a ? a : x
end
deg_to_rad(degrees) click to toggle source
# File lib/mittsu/math.rb, line 77
def self.deg_to_rad(degrees)
  degrees * DEGREE_TO_RADIANS_FACTOR
end
map_linear(x, a1, a2, b1, b2) click to toggle source
# File lib/mittsu/math.rb, line 38
def self.map_linear(x, a1, a2, b1, b2)
  b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 )
end
next_power_of_two(value) click to toggle source
# File lib/mittsu/math.rb, line 90
def self.next_power_of_two(value)
            value -= 1
            value |= value >> 1
            value |= value >> 2
            value |= value >> 4
            value |= value >> 8
            value |= value >> 16
            value += 1
end
power_of_two?(value) click to toggle source
# File lib/mittsu/math.rb, line 86
def self.power_of_two?(value)
  ( value & ( value - 1 ) ) == 0 && value != 0
end
rad_to_deg(radians) click to toggle source
# File lib/mittsu/math.rb, line 82
def self.rad_to_deg(radians)
  radians * RADIANS_TO_DEGREES_FACTOR
end
rand_float(low, high) click to toggle source
# File lib/mittsu/math.rb, line 68
def self.rand_float(low, high)
  low + rand * ( high - low )
end
rand_float_spread(range) click to toggle source
# File lib/mittsu/math.rb, line 72
def self.rand_float_spread(range)
  range * ( 0.5 - rand )
end
rand_int(low, high) click to toggle source
# File lib/mittsu/math.rb, line 64
def self.rand_int(low, high)
  self.rand_float( low, high ).floor
end
random16() click to toggle source
# File lib/mittsu/math.rb, line 60
def self.random16
  ( 65280 * rand + 255 * rand ) / 65535
end
sign(x) click to toggle source
# File lib/mittsu/math.rb, line 23
def self.sign(x)
  return Float::NAN unless x.is_a? Numeric
  return Float::NAN if x.to_f.nan?
  return x.to_f if x.zero?
  return (x < 0) ? -1.0 : (x > 0) ? 1.0 : +x
end
smooth_step(x, min, max) click to toggle source
# File lib/mittsu/math.rb, line 42
def self.smooth_step(x, min, max)
            return 0.0 if x <= min
            return 1.0 if x >= max

            x = ( x - min ) / ( max - min )

       x * x * ( 3.0 - 2.0 * x )
end
smoother_step(x, min, max) click to toggle source
# File lib/mittsu/math.rb, line 51
def self.smoother_step(x, min, max)
            return 0.0 if x <= min
            return 1.0 if x >= max

            x = ( x - min ) / ( max - min )

            x * x * x * ( x * ( x * 6.0 - 15.0 ) + 10.0 )
end