Sha256: 3ac8fcd363dc718e9dc36a46897e186b8d3bd1c2feb72e55a64f6bf48ea83a7a

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# Rectification maps values to an limits treated either as cycles, upper 
# bounds, lower bounds or both. Four methods are added to the Numeric class:
#
# wrap_rectify() wraps values to an interval treated as a cycle. Think of it
# as degrees around a compass.
#
# cut_rectify() cuts off values to keep them between an upper and lower bound.
#
# at_most() cuts off values at an upper bound.
#
# at_least() cuts off value at a lower bound.

# Behavior added to Numeric by eymiha_math.
class Numeric

  # Treats the interval between low and high as a cycle, returning the result
  # of wrapping the instance around this interval.
  def wrap_rectify(high=1,low=0)
    if (high != low)
      low,high = high,low if low > high
      diff = high-low
      self-diff*((self-low)/diff).floor
    else
      low
    end
  end
  
  # Treats the low and high as limits, returning low or high if the instance
  # is respectively below or above these values, or the instance if between
  # the two.
  def cut_rectify(high=1,low=0)
    low,high = high,low if low > high
    (self < low)? low : (self > high)? high : self
  end
  
  # Constrains a value to an upper limit.
  def at_most(high=0)
    self > high ? high : self
  end

  # Constrains a value to a lower limit.
  def at_least(low=0)
    self < low ? low : self
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eymiha_math-0.1.1 lib/rectify.rb
eymiha_math-1.0.0 lib/eymiha/math/rectify.rb
eymiha_math-1.0.1 lib/eymiha/math/rectify.rb