Class: LinearInterpolator
- Inherits:
-
Object
- Object
- LinearInterpolator
- Defined in:
- lib/linear_interpolator.rb
Overview
This provides basic linear interpolation between any number of arbitrarily defined points. It is used for fuzzy logic. Although, fuzzy logic based on a statistically normal curve provides best results, linear interpolation provides a “gud ‘nuf” approach.
Instance Method Summary collapse
Instance Method Details
#add_point(x, y) ⇒ Object
:nodoc:
5 6 7 8 9 |
# File 'lib/linear_interpolator.rb', line 5 def add_point(x,y) @points ||= [] @points << [x,y] @points = @points.sort_by { |x| x[0] } end |
#value_at(x) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/linear_interpolator.rb', line 10 def value_at(x) return 0 if @points.nil? return @points[0][1] if x < @points[0][0] return @points[-1][1] if x > @points[-1][0] after,before=nil,nil @points.each_with_index do |point,i| if(point[0] >= x) after = point before = @points[i-1] end end m=(before[1]-after[1])/(before[0]-after[0]).to_f x-=before[0] b=before[1] return m*x+b end |