Class: Spliner::SplinerSection

Inherits:
Object
  • Object
show all
Defined in:
lib/spliner/spliner_section.rb

Overview

Spliner::SplinerSection is only used via Spliner::Spliner

As the spline algorithm does not handle duplicate X values well, the curve is split into two non continuous parts where duplicate X values appear. Each such part is represented by a SplinerSection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ SplinerSection

Returns a new instance of SplinerSection.



14
15
16
17
18
19
# File 'lib/spliner/spliner_section.rb', line 14

def initialize(x, y)
  @x, @y = x, y
  @x_pairs = @x.each_cons(2).map {|pair| pair.first..pair.last }
  check_points_increasing
  calculate_a_k
end

Instance Attribute Details

#kObject (readonly)

Returns the value of attribute k.



12
13
14
# File 'lib/spliner/spliner_section.rb', line 12

def k
  @k
end

#xObject (readonly)

Returns the value of attribute x.



12
13
14
# File 'lib/spliner/spliner_section.rb', line 12

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



12
13
14
# File 'lib/spliner/spliner_section.rb', line 12

def y
  @y
end

Instance Method Details

#get(v) ⇒ Object

returns an interpolated value



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spliner/spliner_section.rb', line 56

def get(v)
  i = @x_pairs.find_index {|pair| pair.member? v }
  if i
    dx = @x[i + 1] - @x[i]
    dy = @y[i + 1] - @y[i]
    t = (v - @x[i]) / dx
    a = @k[i] * dx - dy
    b = -(@k[i + 1] * dx - dy)
    (1 - t) * @y[i] + t * @y[i + 1] + t * (1 - t) * (a * (1 - t) + b * t)
  elsif @x.size == 1 && @x.first == v
    @y.first
  else
    nil
  end
end

#rangeObject



21
22
23
# File 'lib/spliner/spliner_section.rb', line 21

def range
  @x.first..@x.last
end