class FuzzyAssociativeMemory::Trapezoid

Attributes

height[R]
left[R]
right[R]
top_left[R]
top_right[R]

Public Class Methods

new(left, top_left, top_right, right, height=1.0) click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 14
def initialize(left, top_left, top_right, right, height=1.0)
  # TODO validations

  @left      = left.to_f
  @top_left  = top_left.to_f
  @top_right = top_right.to_f
  @right     = right.to_f
  @height    = height.to_f
end

Public Instance Methods

centroid_x() click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 37
def centroid_x
  a = @top_right - @top_left
  b = @right - @left
  c = @top_left - @left

  cx = (2.0*a*c + a**2 + c*b + a*b + b**2.0) / (3.0 * (a+b))
  cx + @left
  # cy = (@height * (2.0*a + b)) / (3.0 * (a+b))
  # [cx+@left, cy]
end
height=(new_height) click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 48
def height=(new_height)
  @height = new_height
end
larsen(ratio) click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 52
def larsen(ratio)
  t = self.dup
  t.height=(t.height * ratio)
  t
end
mamdani(clip_height) click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 58
def mamdani(clip_height)
  left      = @left
  top_left  = @left + (clip_height * (@top_left - @left))
  top_right = @right - (clip_height * (@right - @top_right))
  right     = @right

  FuzzyAssociativeMemory::Trapezoid.new(left, top_left, top_right, right, clip_height)
end
mu(value) click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 24
def mu(value)
  raise ArgumentError, "value passed to Trapezoid::mu() cannot be nil" if value.nil?
  if value < @left || value > @right
    0.0
  elsif value >= @left && value < @top_left
    (value - @left) / (@top_left - @left)
  elsif value >= @top_left && value <= @top_right
    1.0
  else
    (@right - value) / (@right - @top_right)
  end
end
to_s() click to toggle source
# File lib/fuzzy_associative_memory/trapezoid.rb, line 67
def to_s
  "Trapezoid {#{@left}/#{@top_left}/#{@top_right}/#{@right}, height #{@height}}"
end