interpolate 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +2 -0
- data/CHANGELOG.txt +28 -0
- data/{MIT-LICENSE → LICENSE.txt} +0 -0
- data/Manifest.txt +2 -2
- data/README.txt +180 -5
- data/Rakefile +3 -5
- data/lib/interpolate.rb +81 -229
- data/test/test_all.rb +4 -4
- metadata +30 -7
- metadata.gz.sig +0 -0
- data/CHANGELOG +0 -17
data.tar.gz.sig
ADDED
data/CHANGELOG.txt
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
== 0.2.1 (2008.1.27)
|
2
|
+
|
3
|
+
* First public release
|
4
|
+
|
5
|
+
* Project Cleanup:
|
6
|
+
|
7
|
+
* Documentation enhancements and updates.
|
8
|
+
* +add+ is now +merge+
|
9
|
+
|
10
|
+
== 0.2.0 (2008.1.24)
|
11
|
+
|
12
|
+
* Changed the library name to "interpolate"
|
13
|
+
* Added +interpolate+ (+Array+) that covers uniform arrays and nested arrays
|
14
|
+
* Added more tests, documentation, and examples
|
15
|
+
|
16
|
+
== 0.1.0 (2008.1.22)
|
17
|
+
|
18
|
+
* 2 Major Changes:
|
19
|
+
|
20
|
+
* Gadient calls +interpolate+ on values for OOP goodness
|
21
|
+
* Checks added for respond_to? +interpolate+ on values
|
22
|
+
* Added +interpolate+ (+Numeric+)
|
23
|
+
|
24
|
+
== 0.0.1 (2008.1.20)
|
25
|
+
|
26
|
+
* Initial coding
|
27
|
+
* N-sized arbitrary floating point gradients
|
28
|
+
|
data/{MIT-LICENSE → LICENSE.txt}
RENAMED
File without changes
|
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -1,11 +1,186 @@
|
|
1
|
-
|
1
|
+
= Interpolate
|
2
2
|
|
3
|
-
|
3
|
+
== Author
|
4
|
+
|
5
|
+
Adam Collins [adam.w.collins@gmail.com]
|
6
|
+
|
7
|
+
|
8
|
+
== Description
|
9
|
+
|
10
|
+
Library for generic Interpolation objects. Useful for such things as generating
|
4
11
|
linear motion between points (or arrays of points), multi-channel color
|
5
12
|
gradients, piecewise functions, or even just placing values within intervals.
|
6
13
|
|
7
|
-
== Author
|
8
14
|
|
9
|
-
|
10
|
-
|
15
|
+
== General Usage
|
16
|
+
|
17
|
+
Specify the interpolation as a Hash, where keys represent numeric points
|
18
|
+
along the gradient and values represent the known values along that gradient.
|
19
|
+
|
20
|
+
Here's an example for determining where, in a range of seven zones, each value
|
21
|
+
of a set falls into:
|
22
|
+
|
23
|
+
require 'rubygems'
|
24
|
+
require 'interpolate'
|
25
|
+
|
26
|
+
points = {
|
27
|
+
0.000 => 0,
|
28
|
+
0.427 => 1,
|
29
|
+
1.200 => 2,
|
30
|
+
3.420 => 3,
|
31
|
+
27.50 => 4,
|
32
|
+
45.20 => 5,
|
33
|
+
124.4 => 6,
|
34
|
+
}
|
35
|
+
|
36
|
+
zones = Interpolation.new(points)
|
37
|
+
|
38
|
+
values = [
|
39
|
+
-20.2,
|
40
|
+
0.234,
|
41
|
+
65.24,
|
42
|
+
9.234,
|
43
|
+
398.4,
|
44
|
+
4000
|
45
|
+
]
|
46
|
+
|
47
|
+
values.each do |value|
|
48
|
+
zone = zones.at(value).floor
|
49
|
+
puts "A value of #{value} falls into zone #{zone}"
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
== Non-Numeric Gradients
|
54
|
+
|
55
|
+
For non-Numeric gradient value objects, you'll need to implement +interpolate+
|
56
|
+
for the class in question. Here's an example using an RGB color gradient with
|
57
|
+
the help of the 'color' gem:
|
58
|
+
|
59
|
+
require 'rubygems'
|
60
|
+
require 'interpolate'
|
61
|
+
require 'color'
|
62
|
+
|
63
|
+
# we need to implement +interpolate+ for Color::RGB
|
64
|
+
# in order for Interpolation to work
|
65
|
+
class Color::RGB
|
66
|
+
def interpolate(other, balance)
|
67
|
+
mix_with(other, balance * 100.0)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# a nice weathermap-style color gradient
|
72
|
+
points = {
|
73
|
+
0 => Color::RGB::White,
|
74
|
+
1 => Color::RGB::Lime,
|
75
|
+
# 2 => ? (something between Lime and Yellow)
|
76
|
+
3 => Color::RGB::Yellow,
|
77
|
+
4 => Color::RGB::Orange,
|
78
|
+
5 => Color::RGB::Red,
|
79
|
+
6 => Color::RGB::Magenta,
|
80
|
+
7 => Color::RGB::DarkGray
|
81
|
+
}
|
82
|
+
|
83
|
+
gradient = Interpolation.new(points)
|
84
|
+
|
85
|
+
# what are the colors of the gradient from 0 to 7
|
86
|
+
# in increments of 0.2?
|
87
|
+
(0).step(7, 0.2) do |value|
|
88
|
+
color = gradient.at(value)
|
89
|
+
puts "A value of #{value} means #{color.html}"
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
== Array-based Interpolations
|
94
|
+
|
95
|
+
Aside from single value gradient points, you can interpolate over uniformly sized
|
96
|
+
arrays. Between two interpolation points, let's say +a+ and +b+, the final result
|
97
|
+
will be +c+ where <tt>c[0]</tt> is the interpolation of <tt>a[0]</tt> and
|
98
|
+
<tt>b[0]</tt> and <tt>c[1]</tt> is interpolated between <tt>a[1]</tt> and
|
99
|
+
<tt>b[1]</tt> and so on up to <tt>c[n]</tt>.
|
100
|
+
|
101
|
+
Here is an example:
|
102
|
+
|
103
|
+
require 'rubygems'
|
104
|
+
require 'interpolate'
|
105
|
+
require 'pp'
|
106
|
+
|
107
|
+
# a non-linear set of multi-dimensional points;
|
108
|
+
# perhaps the location of some actor in relation to time
|
109
|
+
time_frames = {
|
110
|
+
0 => [0, 0, 0],
|
111
|
+
1 => [1, 0, 0],
|
112
|
+
2 => [0, 1, 0],
|
113
|
+
3 => [0, 0, 2],
|
114
|
+
4 => [3, 0, 1],
|
115
|
+
5 => [1, 2, 3],
|
116
|
+
6 => [0, 0, 0]
|
117
|
+
}
|
118
|
+
|
119
|
+
path = Interpolation.new(time_frames)
|
120
|
+
|
121
|
+
# play the actors positions in time increments of 0.25
|
122
|
+
(0).step(6, 0.25) do |time|
|
123
|
+
position = path.at(time)
|
124
|
+
puts ">> At #{time}s, actor is at:"
|
125
|
+
p position
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
== Nested Array Interpolations
|
130
|
+
|
131
|
+
As long as each top level array is uniformly sized in the first dimension
|
132
|
+
and each nested array is uniformly sized in the second dimension (and so
|
133
|
+
on...), multidimensional interpolation point values will just work.
|
134
|
+
|
135
|
+
Here's an example of a set of 2D points being morphed:
|
136
|
+
|
137
|
+
require 'rubygems'
|
138
|
+
require 'interpolate'
|
139
|
+
require 'pp'
|
140
|
+
|
141
|
+
|
142
|
+
# a non-linear set of 2D vertexes;
|
143
|
+
# the shape changes at each frame
|
144
|
+
time_frames = {
|
145
|
+
0 => [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]], # a horizontal line
|
146
|
+
1 => [[0, 0], [1, 0], [3, 0], [0, 4], [0, 0]], # a triangle
|
147
|
+
2 => [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]], # a square
|
148
|
+
3 => [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]], # a horizontal line, again
|
149
|
+
4 => [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]] # a vertical line
|
150
|
+
}
|
151
|
+
|
152
|
+
|
153
|
+
paths = Interpolation.new(time_frames)
|
154
|
+
|
155
|
+
# show the vertex positions in time increments of 0.25
|
156
|
+
(0).step(4, 0.25) do |time|
|
157
|
+
points = paths.at(time)
|
158
|
+
puts ">> At #{time}s, points are:"
|
159
|
+
p points
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
== License
|
164
|
+
|
165
|
+
(The MIT License)
|
166
|
+
|
167
|
+
Copyright (c) 2008 Adam Collins [adam.w.collins@gmail.com]
|
168
|
+
|
169
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
170
|
+
a copy of this software and associated documentation files (the
|
171
|
+
"Software"), to deal in the Software without restriction, including
|
172
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
173
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
174
|
+
permit persons to whom the Software is furnished to do so, subject to
|
175
|
+
the following conditions:
|
176
|
+
|
177
|
+
The above copyright notice and this permission notice shall be
|
178
|
+
included in all copies or substantial portions of the Software.
|
11
179
|
|
180
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
181
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
182
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
183
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
184
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
185
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
186
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -6,14 +6,12 @@ require 'interpolate'
|
|
6
6
|
Hoe.new('Interpolate', Interpolation::VERSION) do |p|
|
7
7
|
p.name = "interpolate"
|
8
8
|
p.author = "Adam Collins"
|
9
|
-
p.description = "Library for creating generic interpolations objects."
|
10
9
|
p.email = 'adam.w.collins@gmail.com'
|
11
|
-
p.summary = "Useful for such things as generating linear motion between points (or arrays of points), multi-channel color gradients, piecewise functions, or even just placing values within intervals."
|
12
10
|
p.url = "http://interpolate.rubyforge.org"
|
13
|
-
|
11
|
+
p.description = File.read('README.txt').delete("\r").split(/^== /)[2].chomp.chomp
|
12
|
+
p.summary = p.description
|
13
|
+
p.changes = File.read('CHANGELOG.txt').delete("\r").split(/^== /)[1].chomp
|
14
14
|
p.remote_rdoc_dir = '' # Release to root
|
15
|
-
p.changes = p.paragraphs_of('CHANGELOG', 0..1).join("\n\n")
|
16
|
-
# * extra_deps - An array of rubygem dependencies.
|
17
15
|
end
|
18
16
|
|
19
17
|
desc "Release and publish documentation"
|
data/lib/interpolate.rb
CHANGED
@@ -1,245 +1,58 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
Specify the interpolation as a Hash, where keys represent numeric points
|
33
|
-
along the gradient and values represent the known values along that gradient.
|
34
|
-
|
35
|
-
Here's an example for determining which of 7 zones a set of values fall into:
|
36
|
-
|
37
|
-
require 'rubygems'
|
38
|
-
require 'interpolate'
|
39
|
-
|
40
|
-
points = {
|
41
|
-
0.000 => 0,
|
42
|
-
0.427 => 1,
|
43
|
-
1.200 => 2,
|
44
|
-
3.420 => 3,
|
45
|
-
27.50 => 4,
|
46
|
-
45.20 => 5,
|
47
|
-
124.4 => 6,
|
48
|
-
}
|
49
|
-
|
50
|
-
zones = Interpolation.new(points)
|
51
|
-
|
52
|
-
values = [
|
53
|
-
-20.2,
|
54
|
-
0.234,
|
55
|
-
65.24,
|
56
|
-
9.234,
|
57
|
-
398.4,
|
58
|
-
4000
|
59
|
-
]
|
60
|
-
|
61
|
-
values.each do |value|
|
62
|
-
zone = zones.at(value).floor
|
63
|
-
puts "A value of #{value} falls into zone #{zone}"
|
64
|
-
end
|
65
|
-
|
66
|
-
|
67
|
-
==Non-Numeric Gradients
|
68
|
-
|
69
|
-
For non-Numeric gradient value objects, you'll need to implement :interpolate
|
70
|
-
for the class in question. Here's an example using an RGB color gradient with
|
71
|
-
the help of the 'color' gem:
|
72
|
-
|
73
|
-
require 'rubygems'
|
74
|
-
require 'interpolate'
|
75
|
-
require 'color'
|
76
|
-
|
77
|
-
# we need to implement :interpolate for Color::RGB
|
78
|
-
# in order for Interpolation to work
|
79
|
-
class Color::RGB
|
80
|
-
def interpolate(other, balance)
|
81
|
-
mix_with(other, balance * 100.0)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
# a nice weathermap-style color gradient
|
86
|
-
points = {
|
87
|
-
0 => Color::RGB::White,
|
88
|
-
1 => Color::RGB::Lime,
|
89
|
-
# 2 => ? (something between Lime and Yellow)
|
90
|
-
3 => Color::RGB::Yellow,
|
91
|
-
4 => Color::RGB::Orange,
|
92
|
-
5 => Color::RGB::Red,
|
93
|
-
6 => Color::RGB::Magenta,
|
94
|
-
7 => Color::RGB::DarkGray
|
95
|
-
}
|
96
|
-
|
97
|
-
|
98
|
-
gradient = Interpolation.new(points)
|
99
|
-
|
100
|
-
# what are the colors of the gradient from 0 to 7
|
101
|
-
# in increments of 0.2?
|
102
|
-
(0).step(7, 0.2) do |value|
|
103
|
-
color = gradient.at(value)
|
104
|
-
puts "A value of #{value} means #{color.html}"
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
==Array-based Interpolations
|
109
|
-
|
110
|
-
Aside from single value gradient points, you can interpolate over uniformly sized
|
111
|
-
arrays. Between two interpolation points, let's say +a+ and +b+, the final
|
112
|
-
result will be +c+ where +c[0]+ is the interpolation of +a[0]+ and +b[0]+ and
|
113
|
-
+c[1]+ is interpolated between +a[1]+ and +b[1]+ and so on up to +c[n]+.
|
114
|
-
|
115
|
-
Here is an example:
|
116
|
-
|
117
|
-
require 'rubygems'
|
118
|
-
require 'interpolate'
|
119
|
-
require 'pp'
|
120
|
-
|
121
|
-
# a non-linear set of multi-dimensional points;
|
122
|
-
# perhaps the location of some actor in relation to time
|
123
|
-
time_frames = {
|
124
|
-
0 => [0, 0, 0],
|
125
|
-
1 => [1, 0, 0],
|
126
|
-
2 => [0, 1, 0],
|
127
|
-
3 => [0, 0, 2],
|
128
|
-
4 => [3, 0, 1],
|
129
|
-
5 => [1, 2, 3],
|
130
|
-
6 => [0, 0, 0]
|
131
|
-
}
|
132
|
-
|
133
|
-
path = Interpolation.new(time_frames)
|
134
|
-
|
135
|
-
# play the actors positions in time increments of 0.25
|
136
|
-
(0).step(6, 0.25) do |time|
|
137
|
-
position = path.at(time)
|
138
|
-
puts ">> At #{time}s, actor is at:"
|
139
|
-
p position
|
140
|
-
end
|
141
|
-
|
142
|
-
|
143
|
-
==Nested Array Interpolations
|
144
|
-
|
145
|
-
As long as each top level array is uniformly sized in the first dimension
|
146
|
-
and each nested array is uniformly sized in the second dimension (and so
|
147
|
-
on...), multidimensional interpolation point values will just work.
|
148
|
-
|
149
|
-
Here's an example of a set of 2D points being morphed:
|
150
|
-
|
151
|
-
require 'rubygems'
|
152
|
-
require 'interpolate'
|
153
|
-
require 'pp'
|
154
|
-
|
155
|
-
|
156
|
-
# a non-linear set of 2D vertexes;
|
157
|
-
# the shape changes at each frame
|
158
|
-
time_frames = {
|
159
|
-
0 => [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]], # a horizontal line
|
160
|
-
1 => [[0, 0], [1, 0], [3, 0], [0, 4], [0, 0]], # a triangle
|
161
|
-
2 => [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]], # a square
|
162
|
-
3 => [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]], # a horizontal line, again
|
163
|
-
4 => [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]] # a vertical line
|
164
|
-
}
|
165
|
-
|
166
|
-
|
167
|
-
paths = Interpolation.new(time_frames)
|
168
|
-
|
169
|
-
# show the vertex positions in time increments of 0.25
|
170
|
-
(0).step(4, 0.25) do |time|
|
171
|
-
points = paths.at(time)
|
172
|
-
puts ">> At #{time}s, points are:"
|
173
|
-
p points
|
174
|
-
end
|
175
|
-
|
176
|
-
|
177
|
-
==License
|
178
|
-
|
179
|
-
Licensed under the MIT license.
|
180
|
-
|
181
|
-
=end
|
182
|
-
|
183
|
-
|
184
|
-
# all numeric objects should be supported out of the box
|
185
|
-
class Numeric
|
186
|
-
def interpolate(other, balance)
|
187
|
-
left = self.to_f
|
188
|
-
right = other.to_f
|
189
|
-
delta = (right - left).to_f
|
190
|
-
return left + (delta * balance)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
|
195
|
-
# a little more complicated, but there's no reason why we can't
|
196
|
-
# interpolate between two equal length arrays as long as each element
|
197
|
-
# responds to :interpolate
|
198
|
-
class Array
|
199
|
-
def interpolate(other, balance)
|
200
|
-
if (self.length < 1) then
|
201
|
-
raise ArgumentError, "cannot interpolate array with no values"
|
202
|
-
end
|
203
|
-
|
204
|
-
if (self.length != other.length) then
|
205
|
-
raise ArgumentError, "cannot interpolate between arrays of different length"
|
206
|
-
end
|
207
|
-
|
208
|
-
final = Array.new
|
209
|
-
|
210
|
-
self.each_with_index do |left, index|
|
211
|
-
unless (left.respond_to? :interpolate) then
|
212
|
-
raise "array element does not respond to :interpolate"
|
213
|
-
end
|
214
|
-
|
215
|
-
right = other[index]
|
216
|
-
|
217
|
-
final[index] = left.interpolate(right, balance)
|
218
|
-
end
|
219
|
-
|
220
|
-
return final
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
1
|
+
# Library for generic interpolation objects. Useful for such things as generating
|
2
|
+
# linear motion between points (or arrays of points), multi-channel color
|
3
|
+
# gradients, piecewise functions, or even just placing values within intervals.
|
4
|
+
#
|
5
|
+
# The only requirement is that each interpolation point value must be able to
|
6
|
+
# figure out how to interpolate itself to its neighbor value(s). Numeric
|
7
|
+
# objects and uniformly sized arrays are automatically endowed with this
|
8
|
+
# ability by this gem, but other classes will require an implementation
|
9
|
+
# of +interpolate+. See the example color.rb in the examples directory for
|
10
|
+
# a brief demonstration using Color objects provided by the 'color' gem.
|
11
|
+
#
|
12
|
+
# Interpolation objects are constructed with a Hash object, wherein each key
|
13
|
+
# is a real number value and each value is can respond to +interpolate+ and
|
14
|
+
# determine the resulting value based on its neighbor value and the balance
|
15
|
+
# ratio between the two points.
|
16
|
+
#
|
17
|
+
# At or below the lower bounds of the interpolation, the result will be equal to
|
18
|
+
# the value of the lower bounds interpolation point. At or above the upper
|
19
|
+
# bounds of the graient, the result will be equal to the value of the upper
|
20
|
+
# bounds interpolation point.
|
21
|
+
#
|
22
|
+
#
|
23
|
+
# ==Author
|
24
|
+
#
|
25
|
+
# {Adam Collins}[mailto:adam.w.collins@gmail.com]
|
26
|
+
#
|
27
|
+
#
|
28
|
+
# ==License
|
29
|
+
#
|
30
|
+
# Licensed under the MIT license.
|
31
|
+
#
|
224
32
|
|
225
33
|
class Interpolation
|
226
|
-
VERSION = '0.2.
|
34
|
+
VERSION = '0.2.1' # :nodoc:
|
227
35
|
|
36
|
+
# creates an Interpolation object with Hash object that specifies
|
37
|
+
# each point location (Numeric) and value (up to you)
|
228
38
|
def initialize(points = {})
|
229
39
|
@points = {}
|
230
|
-
|
40
|
+
merge!(points)
|
231
41
|
end
|
232
42
|
|
233
|
-
|
43
|
+
# creates an Interpolation object from the receiver object,
|
44
|
+
# merged with the interpolated points you specify
|
45
|
+
def merge(points = {})
|
234
46
|
Interpolation.new(points.merge(@points))
|
235
47
|
end
|
236
48
|
|
237
|
-
|
49
|
+
# merges the interpolation points with the receiver object
|
50
|
+
def merge!(points = {})
|
238
51
|
@points.merge!(points)
|
239
52
|
normalize_data
|
240
53
|
end
|
241
54
|
|
242
|
-
|
55
|
+
# returns the interpolated value of the receiver object at the point specified
|
243
56
|
def at(point)
|
244
57
|
# deal with the two out-of-bounds cases first
|
245
58
|
if (point <= @min_point)
|
@@ -285,7 +98,7 @@ class Interpolation
|
|
285
98
|
|
286
99
|
private
|
287
100
|
|
288
|
-
def normalize_data
|
101
|
+
def normalize_data # :nodoc:
|
289
102
|
@data = @points.sort
|
290
103
|
@min_point = @data.first.first
|
291
104
|
@max_point = @data.last.first
|
@@ -302,3 +115,42 @@ class Interpolation
|
|
302
115
|
end
|
303
116
|
|
304
117
|
|
118
|
+
# all numeric objects should be supported
|
119
|
+
class Numeric # :nodoc:
|
120
|
+
def interpolate(other, balance)
|
121
|
+
left = self.to_f
|
122
|
+
right = other.to_f
|
123
|
+
delta = (right - left).to_f
|
124
|
+
return left + (delta * balance)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
# a little more complicated, but there's no reason why we can't
|
130
|
+
# interpolate between two equal length arrays as long as each element
|
131
|
+
# responds to +interpolate+
|
132
|
+
class Array # :nodoc:
|
133
|
+
def interpolate(other, balance)
|
134
|
+
if (self.length < 1) then
|
135
|
+
raise ArgumentError, "cannot interpolate array with no values"
|
136
|
+
end
|
137
|
+
|
138
|
+
if (self.length != other.length) then
|
139
|
+
raise ArgumentError, "cannot interpolate between arrays of different length"
|
140
|
+
end
|
141
|
+
|
142
|
+
final = Array.new
|
143
|
+
|
144
|
+
self.each_with_index do |left, index|
|
145
|
+
unless (left.respond_to? :interpolate) then
|
146
|
+
raise "array element does not respond to :interpolate"
|
147
|
+
end
|
148
|
+
|
149
|
+
right = other[index]
|
150
|
+
|
151
|
+
final[index] = left.interpolate(right, balance)
|
152
|
+
end
|
153
|
+
|
154
|
+
return final
|
155
|
+
end
|
156
|
+
end
|
data/test/test_all.rb
CHANGED
@@ -79,7 +79,7 @@ class InterpolationTest < Test::Unit::TestCase
|
|
79
79
|
assert_in_delta(@dec_gradient.at(3.5701), 0.35701, DELTA)
|
80
80
|
end
|
81
81
|
|
82
|
-
def
|
82
|
+
def test_gradient_merge
|
83
83
|
new_points = {
|
84
84
|
11 => 1.1,
|
85
85
|
12 => 1.2,
|
@@ -94,7 +94,7 @@ class InterpolationTest < Test::Unit::TestCase
|
|
94
94
|
}
|
95
95
|
|
96
96
|
original = @dec_gradient.dup
|
97
|
-
expanded = original.
|
97
|
+
expanded = original.merge(new_points)
|
98
98
|
|
99
99
|
assert_equal(original.at(5), 0.5)
|
100
100
|
assert_equal(expanded.at(5), 0.5)
|
@@ -103,7 +103,7 @@ class InterpolationTest < Test::Unit::TestCase
|
|
103
103
|
assert_equal(expanded.at(15), 1.5)
|
104
104
|
end
|
105
105
|
|
106
|
-
def
|
106
|
+
def test_gradient_merge!
|
107
107
|
new_points = {
|
108
108
|
11 => 1.1,
|
109
109
|
12 => 1.2,
|
@@ -119,7 +119,7 @@ class InterpolationTest < Test::Unit::TestCase
|
|
119
119
|
|
120
120
|
original = @dec_gradient.dup
|
121
121
|
expanded = original.dup
|
122
|
-
expanded.
|
122
|
+
expanded.merge!(new_points)
|
123
123
|
|
124
124
|
assert_equal(original.at(5), 0.5)
|
125
125
|
assert_equal(expanded.at(5), 0.5)
|
metadata
CHANGED
@@ -1,15 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interpolate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Collins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
|
-
cert_chain:
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDPjCCAiagAwIBAgIBADANBgkqhkiG9w0BAQUFADBFMRcwFQYDVQQDDA5hZGFt
|
14
|
+
LncuY29sbGluczEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQB
|
15
|
+
GRYDY29tMB4XDTA4MDEyNDA4NTEyOFoXDTA5MDEyMzA4NTEyOFowRTEXMBUGA1UE
|
16
|
+
AwwOYWRhbS53LmNvbGxpbnMxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmS
|
17
|
+
JomT8ixkARkWA2NvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMP8
|
18
|
+
3Nz8g1K+Z4p59/keyov5ihFgzJuhlnvbRLr0y2jtDybeyc0TSMnY7sjhx+T0yVI5
|
19
|
+
9tEtUMo3d2m0woaHw7kc3VAo68GQKGAN02bdaPc4ODcObL9DAr8Y3CCwml5CiLtX
|
20
|
+
L5Lz8zO9EU6jv6bTecRW5DsY9nPjc/TLqXjYxDgSL8dppBmHs2k82bJCaCaTFj9M
|
21
|
+
ORRZpdkdTBdecI7p8DKt2WAX12deT/XUan7mpiBChKJsNVpcAe6CUqVfb6hMGA95
|
22
|
+
KtW+GxvK6F6UgkcFG8ljrXks8Dfm4jMYVkpmw1YUGYU9Wj9R8X94ecAi9By/ngJ7
|
23
|
+
svhO6ouvVk9J9hgpWukCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAw
|
24
|
+
HQYDVR0OBBYEFIpBCufIdqpuVqh/Mt46Gq9FKa0pMA0GCSqGSIb3DQEBBQUAA4IB
|
25
|
+
AQBbwpirH1tNzkESxQIBZd10xK3Dca141G9+lHl7OK3UCk1ZF6TiXxgl7Qnug4A5
|
26
|
+
3mEn/catvIdMYcA1GLNWL7qlW2Fpk2w0qgVbx1agK724BrZm5Op6lain+vi6BXd3
|
27
|
+
QM32MqZAL96e40i6UCutsNIdZeRDfR7hRcmqPTkoSUEu/3X6qegnvJHpVw36dsG6
|
28
|
+
trlR7ps6/GSlnZNkD5TayaybO3TiA+KGA19/zQhO6DMPds+swW0Jz7xv2VBzIWg5
|
29
|
+
RAnseWJ5nOAftsE9D1NGp3TEH+ceNKO3IP0OtDl9L2F0a/9XbPJdmsaQR+FPX30Z
|
30
|
+
xJc09X9KG2jBdxa4tp+uy7KZ
|
31
|
+
-----END CERTIFICATE-----
|
11
32
|
|
12
|
-
date: 2008-01-
|
33
|
+
date: 2008-01-27 00:00:00 -08:00
|
13
34
|
default_executable:
|
14
35
|
dependencies:
|
15
36
|
- !ruby/object:Gem::Dependency
|
@@ -21,18 +42,20 @@ dependencies:
|
|
21
42
|
- !ruby/object:Gem::Version
|
22
43
|
version: 1.4.0
|
23
44
|
version:
|
24
|
-
description: Library for
|
45
|
+
description: Description Library for generic Interpolation objects. Useful for such things as generating linear motion between points (or arrays of points), multi-channel color gradients, piecewise functions, or even just placing values within intervals.
|
25
46
|
email: adam.w.collins@gmail.com
|
26
47
|
executables: []
|
27
48
|
|
28
49
|
extensions: []
|
29
50
|
|
30
51
|
extra_rdoc_files:
|
52
|
+
- CHANGELOG.txt
|
53
|
+
- LICENSE.txt
|
31
54
|
- Manifest.txt
|
32
55
|
- README.txt
|
33
56
|
files:
|
34
|
-
- CHANGELOG
|
35
|
-
-
|
57
|
+
- CHANGELOG.txt
|
58
|
+
- LICENSE.txt
|
36
59
|
- Manifest.txt
|
37
60
|
- README.txt
|
38
61
|
- Rakefile
|
@@ -68,6 +91,6 @@ rubyforge_project: interpolate
|
|
68
91
|
rubygems_version: 1.0.1
|
69
92
|
signing_key:
|
70
93
|
specification_version: 2
|
71
|
-
summary: Useful for such things as generating linear motion between points (or arrays of points), multi-channel color gradients, piecewise functions, or even just placing values within intervals.
|
94
|
+
summary: Description Library for generic Interpolation objects. Useful for such things as generating linear motion between points (or arrays of points), multi-channel color gradients, piecewise functions, or even just placing values within intervals.
|
72
95
|
test_files:
|
73
96
|
- test/test_all.rb
|
metadata.gz.sig
ADDED
Binary file
|
data/CHANGELOG
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
== 0.2.0
|
2
|
-
|
3
|
-
* Changed the library name to "interpolate"
|
4
|
-
* Added Array#interpolate that covers uniform arrays and nested arrays
|
5
|
-
* Added more tests, documentation, and examples
|
6
|
-
|
7
|
-
== 0.1.0
|
8
|
-
|
9
|
-
* Gadient calls :interpolate on values for OOP goodness
|
10
|
-
* Checks added for respond_to? :interpolate on values
|
11
|
-
* Added Numeric#interpolate
|
12
|
-
|
13
|
-
== 0.0.1
|
14
|
-
|
15
|
-
* Initial coding
|
16
|
-
* N-sized arbitrary floating point gradients
|
17
|
-
|