Class: Scratchpad::Interpolator
- Inherits:
-
Object
- Object
- Scratchpad::Interpolator
- Includes:
- Math
- Defined in:
- lib/scratchpad/interpolator.rb
Defined Under Namespace
Classes: DataPoint
Constant Summary collapse
- X =
0
- Y =
1
Instance Method Summary collapse
-
#feed(x, y, is_down) ⇒ Object
(x, y) -> [[Float, Float, Float]].
- #find_accelerations(s, velocity, target_position, target_velocity) ⇒ Object
-
#initialize ⇒ Interpolator
constructor
A new instance of Interpolator.
- #interpolate ⇒ Object
Constructor Details
#initialize ⇒ Interpolator
Returns a new instance of Interpolator.
9 10 11 12 13 14 15 |
# File 'lib/scratchpad/interpolator.rb', line 9 def initialize @x_v = nil @y_v = nil @x = nil @y = nil @ring = [nil, nil, nil] end |
Instance Method Details
#feed(x, y, is_down) ⇒ Object
(x, y) -> [[Float, Float, Float]]
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/scratchpad/interpolator.rb', line 21 def feed(x, y, is_down) raise unless @ring.size == 3 @ring.shift @ring << [DataPoint.new(nil, x), DataPoint.new(nil, y), is_down] if @ring[1] == nil # cannot calculate yet return [] else # calculate @ring[1]'s velocity if @ring[0] == nil @ring[1][X].v = (@ring[2][X].pos - @ring[1][X].pos) @ring[1][Y].v = (@ring[2][Y].pos - @ring[1][Y].pos) return [] else @ring[1][X].v = ((@ring[1][X].pos - @ring[0][X].pos) + (@ring[2][X].pos - @ring[1][X].pos)) * 0.5 @ring[1][Y].v = ((@ring[1][Y].pos - @ring[0][Y].pos) + (@ring[2][Y].pos - @ring[1][Y].pos)) * 0.5 # unless @ring[1][2] # is_down # return [] # else return interpolate # end end end end |
#find_accelerations(s, velocity, target_position, target_velocity) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/scratchpad/interpolator.rb', line 89 def find_accelerations(s, velocity, target_position, target_velocity) a = 0.0 b = 0.0 s_ = s velocity_ = velocity loop do s = s_ velocity = velocity_ (0..9).map { |n| n*0.1 }.map do |t| [t, s].tap do s += 0.1 * velocity if t < 0.5 velocity += a * 0.1 else velocity += b * 0.1 end end end if (s - target_position).abs >= 0.001 a += -(s - target_position) * 1.0 elsif (velocity - target_velocity).abs >= 0.001 b += -(velocity - target_velocity) * 1.0 else return a, b end end end |
#interpolate ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/scratchpad/interpolator.rb', line 49 def interpolate a, b = find_accelerations(@ring[0][X].pos, @ring[0][X].v, @ring[1][X].pos, @ring[1][X].v) s = @ring[0][X].pos velocity = @ring[0][X].v xpoints = (0..10).map { |n| n*0.1 }.map do |t| [s, velocity].tap do s += 0.1 * velocity if t < 0.5 velocity += a * 0.1 else velocity += b * 0.1 end end end a, b = find_accelerations(@ring[0][Y].pos, @ring[0][Y].v, @ring[1][Y].pos, @ring[1][Y].v) s = @ring[0][Y].pos velocity = @ring[0][Y].v ypoints = (0..10).map { |n| n*0.1 }.map do |t| [s, velocity].tap do s += 0.1 * velocity if t < 0.5 velocity += a * 0.1 else velocity += b * 0.1 end end end return xpoints.zip(ypoints).map { |x_data, y_data| [x_data[0], y_data[0], sqrt(x_data[1] ** 2 + y_data[1] ** 2)] } end |