Module: Mittsu::Math
- Defined in:
- lib/mittsu/math.rb
Constant Summary collapse
- DEGREE_TO_RADIANS_FACTOR =
::Math::PI / 180
- RADIANS_TO_DEGREES_FACTOR =
180 / ::Math::PI
Class Method Summary collapse
- .clamp(x, a, b) ⇒ Object
- .clamp_bottom(x, a) ⇒ Object
- .deg_to_rad(degrees) ⇒ Object
- .map_linear(x, a1, a2, b1, b2) ⇒ Object
- .next_power_of_two(value) ⇒ Object
- .power_of_two?(value) ⇒ Boolean
- .rad_to_deg(radians) ⇒ Object
- .rand_float(low, high) ⇒ Object
- .rand_float_spread(range) ⇒ Object
- .rand_int(low, high) ⇒ Object
- .random16 ⇒ Object
- .sign(x) ⇒ Object
- .smooth_step(x, min, max) ⇒ Object
- .smoother_step(x, min, max) ⇒ Object
Class Method Details
.clamp(x, a, b) ⇒ Object
30 31 32 |
# 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) ⇒ Object
34 35 36 |
# File 'lib/mittsu/math.rb', line 34 def self.clamp_bottom(x, a) x < a ? a : x end |
.deg_to_rad(degrees) ⇒ Object
77 78 79 |
# 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) ⇒ Object
38 39 40 |
# 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) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# 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) ⇒ Boolean
86 87 88 |
# File 'lib/mittsu/math.rb', line 86 def self.power_of_two?(value) ( value & ( value - 1 ) ) == 0 && value != 0 end |
.rad_to_deg(radians) ⇒ Object
82 83 84 |
# File 'lib/mittsu/math.rb', line 82 def self.rad_to_deg(radians) radians * RADIANS_TO_DEGREES_FACTOR end |
.rand_float(low, high) ⇒ Object
68 69 70 |
# File 'lib/mittsu/math.rb', line 68 def self.rand_float(low, high) low + rand * ( high - low ) end |
.rand_float_spread(range) ⇒ Object
72 73 74 |
# File 'lib/mittsu/math.rb', line 72 def self.rand_float_spread(range) range * ( 0.5 - rand ) end |
.rand_int(low, high) ⇒ Object
64 65 66 |
# File 'lib/mittsu/math.rb', line 64 def self.rand_int(low, high) self.rand_float( low, high ).floor end |
.random16 ⇒ Object
60 61 62 |
# File 'lib/mittsu/math.rb', line 60 def self.random16 ( 65280 * rand + 255 * rand ) / 65535 end |
.sign(x) ⇒ Object
23 24 25 26 27 28 |
# 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) ⇒ Object
42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
51 52 53 54 55 56 57 58 |
# 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 |