interpolate 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data.tar.gz.sig CHANGED
Binary file
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.3 (2009.10.27)
2
+
3
+ * Support for passing a block to new Interpolation objects
4
+
1
5
  == 0.2.2 (2008.2.4)
2
6
 
3
7
  * Single source file has been split into Class files
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008 Adam Collins [adam.w.collins@gmail.com]
1
+ Copyright (c) 2008-2009 Adam Collins [adam.w.collins@gmail.com]
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Manifest.txt CHANGED
@@ -8,7 +8,7 @@ examples/colors.rb
8
8
  examples/nested.rb
9
9
  examples/zones.rb
10
10
  lib/interpolate.rb
11
- lib/interpolate/interpolation.rb
12
- lib/interpolate/ruby_array.rb
13
- lib/interpolate/ruby_numeric.rb
11
+ lib/interpolate/add/array.rb
12
+ lib/interpolate/add/core.rb
13
+ lib/interpolate/add/numeric.rb
14
14
  test/test_all.rb
data/README.txt CHANGED
@@ -53,21 +53,13 @@ of a set falls into:
53
53
  == Non-Numeric Gradients
54
54
 
55
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:
56
+ for the class in question or provide a block to the new Interpolation object.
57
+ Here's an example using an RGB color gradient with the help of the 'color' gem:
58
58
 
59
59
  require 'rubygems'
60
60
  require 'interpolate'
61
61
  require 'color'
62
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
63
  # a nice weathermap-style color gradient
72
64
  points = {
73
65
  0 => Color::RGB::White,
@@ -80,7 +72,9 @@ the help of the 'color' gem:
80
72
  7 => Color::RGB::DarkGray
81
73
  }
82
74
 
83
- gradient = Interpolation.new(points)
75
+ gradient = Interpolation.new(points) do |left, right, balance|
76
+ left.mix_with(right, balance * 100.0)
77
+ end
84
78
 
85
79
  # what are the colors of the gradient from 0 to 7
86
80
  # in increments of 0.2?
@@ -90,6 +84,7 @@ the help of the 'color' gem:
90
84
  end
91
85
 
92
86
 
87
+
93
88
  == Array-based Interpolations
94
89
 
95
90
  Aside from single value gradient points, you can interpolate over uniformly sized
@@ -164,7 +159,7 @@ Here's an example of a set of 2D points being morphed:
164
159
 
165
160
  (The MIT License)
166
161
 
167
- Copyright (c) 2008 Adam Collins [adam.w.collins@gmail.com]
162
+ Copyright (c) 2008-2009 Adam Collins [adam.w.collins@gmail.com]
168
163
 
169
164
  Permission is hereby granted, free of charge, to any person obtaining
170
165
  a copy of this software and associated documentation files (the
data/examples/arrays.rb CHANGED
@@ -2,7 +2,6 @@ require 'rubygems'
2
2
  require 'interpolate'
3
3
  require 'pp'
4
4
 
5
-
6
5
  # a non-linear set of multi-dimensional points;
7
6
  # perhaps the location of some actor in relation to time
8
7
  time_frames = {
data/examples/colors.rb CHANGED
@@ -2,15 +2,6 @@ require 'rubygems'
2
2
  require 'interpolate'
3
3
  require 'color'
4
4
 
5
-
6
- # we need to implement :interpolate for Color::RGB
7
- # in order for Interpolation to work
8
- class Color::RGB
9
- def interpolate(other, balance)
10
- mix_with(other, balance * 100.0)
11
- end
12
- end
13
-
14
5
  # a nice weathermap-style color gradient
15
6
  points = {
16
7
  0 => Color::RGB::White,
@@ -23,8 +14,12 @@ points = {
23
14
  7 => Color::RGB::DarkGray
24
15
  }
25
16
 
26
-
27
- gradient = Interpolation.new(points)
17
+ # Color objects can't interpolate themselves right out of the box,
18
+ # so rather than monkey patch the :interpolate method, you can pass
19
+ # a block with the instantiation of a new Interpolation object
20
+ gradient = Interpolation.new(points) do |left, right, balance|
21
+ left.mix_with(right, balance * 100.0)
22
+ end
28
23
 
29
24
  # what are the colors of the gradient from 0 to 7
30
25
  # in increments of 0.2?
data/lib/interpolate.rb CHANGED
@@ -1,3 +1,3 @@
1
- require 'interpolate/interpolation'
2
- require 'interpolate/ruby_array'
3
- require 'interpolate/ruby_numeric'
1
+ require 'interpolate/add/core'
2
+ require 'interpolate/add/array'
3
+ require 'interpolate/add/numeric'
File without changes
@@ -2,18 +2,15 @@
2
2
  # linear motion between points (or arrays of points), multi-channel color
3
3
  # gradients, piecewise functions, or even just placing values within intervals.
4
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
5
  # 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
6
+ # is a real number value and each value can respond to +interpolate+ to
14
7
  # determine the resulting value based on its neighbor value and the balance
15
8
  # ratio between the two points.
16
9
  #
10
+ # For objects which can't respond to +interpolate+ (or to override the default
11
+ # behaviour), a block can be passed to +new+ which will be called whenever two
12
+ # values need to be interpolated.
13
+ #
17
14
  # At or below the lower bounds of the interpolation, the result will be equal to
18
15
  # the value of the lower bounds interpolation point. At or above the upper
19
16
  # bounds of the graient, the result will be equal to the value of the upper
@@ -31,18 +28,26 @@
31
28
  #
32
29
 
33
30
  class Interpolation
34
- VERSION = '0.2.2'
31
+ VERSION = '0.2.3'
35
32
 
36
33
  # creates an Interpolation object with Hash object that specifies
37
34
  # each point location (Numeric) and value (up to you)
38
- def initialize(points = {})
35
+ #
36
+ # the optional+block+ can be used to interpolate objects that can't
37
+ # respond to +interpolate+ on their own
38
+ #
39
+ # +block+ will receive the following arguments: "left" (lower) side
40
+ # value, "right" (higher) side value, and the balance ratio from 0.0
41
+ # to 1.0
42
+ def initialize(points = {}, &block)
39
43
  @points = {}
44
+ @block = block
40
45
  merge!(points)
41
46
  end
42
47
 
43
48
  # creates an Interpolation object from the receiver object,
44
49
  # merged with the interpolated points you specify
45
- def merge(points = {})
50
+ def merge(points = {})
46
51
  Interpolation.new(points.merge(@points))
47
52
  end
48
53
 
@@ -52,7 +57,7 @@ class Interpolation
52
57
  normalize_data
53
58
  end
54
59
 
55
- # returns the interpolated value of the receiver object at the point specified
60
+ # returns the interpolated value of the receiver object at the point specified
56
61
  def at(point)
57
62
  # deal with the two out-of-bounds cases first
58
63
  if (point <= @min_point)
@@ -87,8 +92,13 @@ class Interpolation
87
92
  return left_value if (balance == 0.0)
88
93
  return right_value if (balance == 1.0)
89
94
 
95
+ # given block should be called
96
+ return @block.call(left_value, right_value, balance) if @block
97
+
90
98
  # otherwise, we need to interpolate
91
- return left_value.interpolate(right_value, balance)
99
+ return left_value.interpolate(right_value, balance) if left_value.respond_to?(:interpolate)
100
+
101
+ raise ArgumentError, "no block given and interpolation point doesn't respond to :interpolate"
92
102
  end
93
103
  end
94
104
 
@@ -106,9 +116,6 @@ class Interpolation
106
116
  # make sure that all values respond_to? :interpolate
107
117
  @data.each do |point|
108
118
  value = point.last
109
- unless value.respond_to?(:interpolate)
110
- raise ArgumentError, "found an interpolation point that doesn't respond to :interpolate"
111
- end
112
119
  end
113
120
  end
114
121
 
File without changes
data/test/test_all.rb CHANGED
@@ -9,7 +9,7 @@ class InterpolationTest < Test::Unit::TestCase
9
9
  DELTA = 1e-7
10
10
 
11
11
  def setup
12
- decimal_points = {
12
+ @decimal_points = {
13
13
  0 => 0,
14
14
  1 => 0.1,
15
15
  2 => 0.2,
@@ -23,29 +23,28 @@ class InterpolationTest < Test::Unit::TestCase
23
23
  10 => 1
24
24
  }.freeze
25
25
 
26
- array_points = {
26
+ @array_points = {
27
27
  100 => [1, 10, 100],
28
28
  200 => [5, 50, 500],
29
29
  500 => [10, 100, 1000]
30
30
  }.freeze
31
31
 
32
- @dec_gradient = Interpolation.new(decimal_points).freeze
33
- @array_gradient = Interpolation.new(array_points).freeze
32
+ @dec_gradient = Interpolation.new(@decimal_points).freeze
33
+ @array_gradient = Interpolation.new(@array_points).freeze
34
34
  end
35
35
 
36
-
37
- def test_bad_points
36
+ def test_bad_point
38
37
  bad_points = {
39
38
  0 => 4.2,
40
39
  1 => "hello", # not allowed by default
41
40
  2 => 3.4,
42
41
  3 => 4.8
43
42
  }
43
+ gradient = Interpolation.new(bad_points)
44
44
 
45
45
  assert_raise ArgumentError do
46
- gradient = Interpolation.new(bad_points)
46
+ gradient.at(1.5)
47
47
  end
48
-
49
48
  end
50
49
 
51
50
  def test_lower_bounds
@@ -133,7 +132,31 @@ class InterpolationTest < Test::Unit::TestCase
133
132
  assert_equal(@array_gradient.at(200), [5, 50, 500])
134
133
  assert_equal(@array_gradient.at(350), [7.5, 75, 750])
135
134
  end
136
-
135
+
136
+ def test_given_block_left
137
+ gradient = Interpolation.new(@decimal_points) {|l, r, bal| l}
138
+ assert_in_delta(gradient.at(1.5), 0.1, DELTA)
139
+ assert_in_delta(gradient.at(2.5), 0.2, DELTA)
140
+ assert_in_delta(gradient.at(3.5), 0.3, DELTA)
141
+ end
142
+
143
+ def test_given_block_right
144
+ gradient = Interpolation.new(@decimal_points) {|l, r, bal| r}
145
+ assert_in_delta(gradient.at(1.5), 0.2, DELTA)
146
+ assert_in_delta(gradient.at(2.5), 0.3, DELTA)
147
+ assert_in_delta(gradient.at(3.5), 0.4, DELTA)
148
+ end
149
+
150
+ def test_given_block_mean_squared
151
+ # squared balance, not linear
152
+ gradient = Interpolation.new(@decimal_points) do |l, r, bal|
153
+ l + ((r - l).to_f * bal * bal)
154
+ end
155
+ assert_in_delta(gradient.at(1.5), 0.12500, DELTA)
156
+ assert_in_delta(gradient.at(2.75), 0.25625, DELTA)
157
+ assert_in_delta(gradient.at(3.99), 0.39801, DELTA)
158
+ end
159
+
137
160
  def test_frozen_points
138
161
  a = @array_gradient.at(200)
139
162
  assert_nothing_raised RuntimeError do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interpolate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Collins
@@ -30,19 +30,26 @@ cert_chain:
30
30
  xJc09X9KG2jBdxa4tp+uy7KZ
31
31
  -----END CERTIFICATE-----
32
32
 
33
- date: 2008-02-05 00:00:00 -08:00
33
+ date: 2009-10-27 00:00:00 -07:00
34
34
  default_executable:
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: hoe
38
+ type: :development
38
39
  version_requirement:
39
40
  version_requirements: !ruby/object:Gem::Requirement
40
41
  requirements:
41
42
  - - ">="
42
43
  - !ruby/object:Gem::Version
43
- version: 1.5.0
44
+ version: 2.3.3
44
45
  version:
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.
46
+ description: |
47
+ Description
48
+
49
+ Library for generic Interpolation objects. Useful for such things as generating
50
+ linear motion between points (or arrays of points), multi-channel color
51
+ gradients, piecewise functions, or even just placing values within intervals.
52
+
46
53
  email: adam.w.collins@gmail.com
47
54
  executables: []
48
55
 
@@ -64,12 +71,14 @@ files:
64
71
  - examples/nested.rb
65
72
  - examples/zones.rb
66
73
  - lib/interpolate.rb
67
- - lib/interpolate/interpolation.rb
68
- - lib/interpolate/ruby_array.rb
69
- - lib/interpolate/ruby_numeric.rb
74
+ - lib/interpolate/add/array.rb
75
+ - lib/interpolate/add/core.rb
76
+ - lib/interpolate/add/numeric.rb
70
77
  - test/test_all.rb
71
78
  has_rdoc: true
72
79
  homepage: http://interpolate.rubyforge.org
80
+ licenses: []
81
+
73
82
  post_install_message:
74
83
  rdoc_options:
75
84
  - --main
@@ -91,9 +100,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
100
  requirements: []
92
101
 
93
102
  rubyforge_project: interpolate
94
- rubygems_version: 1.0.1
103
+ rubygems_version: 1.3.5
95
104
  signing_key:
96
- specification_version: 2
105
+ specification_version: 3
97
106
  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.
98
107
  test_files:
99
108
  - test/test_all.rb
metadata.gz.sig CHANGED
Binary file