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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spliner/spliner_section.rb', line 61

def get(v)
  i = @x_pairs.find_index {|pair| pair.cover? v }
  if i
    x_pair = @x_pairs[i]
    x_min = x_pair.min
    dx = x_pair.max - x_min
    y_max = @y[i + 1]
    y_min = @y[i]
    dy = y_max - y_min
    t = (v - x_min) / dx
    a =      @k[i] * dx - dy
    b = -@k[i + 1] * dx + dy
    one_minus_t = 1 - t
    t * y_max + one_minus_t * ( y_min + t * ( a * one_minus_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