Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/interpolate/add/core/array.rb
Overview
Instance Method Summary collapse
-
#interpolate(other, balance) ⇒ Object
Returns a new Array in which each element is the interpolated value between
self
andother
.
Instance Method Details
#interpolate(other, balance) ⇒ Object
Returns a new Array in which each element is the interpolated value between self
and other
. balance
should be a Float from 0.0 to 1.0 where the value is a ratio between self
and other
. self
and other
should be arrays of equal, non-zero length.
Between two interpolation points, let’s say a
and b
, the final result will be c
where c[0]
is the interpolation of a[0]
and b[0]
and c[1]
is interpolated between a[1]
and b[1]
and so on, up to c[c.length - 1]
.
This method is intentionally abstract to allow for the interpolation of nested arrays. In this case, both arrays need to have the same array structure (same number of dimensions, equal length in each dimension), but the contents can, of course, be different.
A balance less than or equal to 0.0 returns self
, while a balance greater than or equal to 1.0 returns other
.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/interpolate/add/core/array.rb', line 32 def interpolate(other, balance) if (self.length < 1) then raise ArgumentError, "cannot interpolate empty array" end if self.length != other.length then raise ArgumentError, "cannot interpolate between arrays of different length" end # catch the easy cases return self.dup if (balance <= 0.0) return other.dup if (balance >= 1.0) final = Array.new self.each_with_index do |value, index| unless value.respond_to? :interpolate then raise "array element does not respond to :interpolate" end final[index] = value.interpolate(other[index], balance) end final end |