Class: Curvature::Curve
- Inherits:
-
Object
- Object
- Curvature::Curve
- Defined in:
- lib/curvature/curve.rb
Constant Summary collapse
- DefaultSampleAttributes =
{ stroke: 'none', fill: 'black', }
Instance Attribute Summary collapse
-
#chart ⇒ Object
Returns the value of attribute chart.
-
#name ⇒ Object
Returns the value of attribute name.
-
#samples ⇒ Object
Returns the value of attribute samples.
Instance Method Summary collapse
- #<<(sample) ⇒ Object
-
#initialize(params = {}) ⇒ Curve
constructor
A new instance of Curve.
- #print ⇒ Object
- #to_html ⇒ Object
- #value_for(input) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Curve
Returns a new instance of Curve.
9 10 11 12 13 |
# File 'lib/curvature/curve.rb', line 9 def initialize(params={}) @samples = [] { }.merge(params).each { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#chart ⇒ Object
Returns the value of attribute chart.
7 8 9 |
# File 'lib/curvature/curve.rb', line 7 def chart @chart end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/curvature/curve.rb', line 5 def name @name end |
#samples ⇒ Object
Returns the value of attribute samples.
6 7 8 |
# File 'lib/curvature/curve.rb', line 6 def samples @samples end |
Instance Method Details
#<<(sample) ⇒ Object
15 16 17 |
# File 'lib/curvature/curve.rb', line 15 def <<(sample) @samples << sample end |
#print ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/curvature/curve.rb', line 29 def print puts @name @samples.each do |sample| puts "\t%2s: %2.2f => %2.2f" % [sample.label, sample.input, sample.output] end puts end |
#to_html ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/curvature/curve.rb', line 42 def to_html xml = Builder::XmlMarkup.new(indent: 2) # draw interpolated curve xml.g(fill: 'none', stroke: 'green', :'stroke-width' => 0.5) do points = @chart.x_range.step(1.0 / @chart.width).map do |input| @chart.scaled_point(input, value_for(input)) end xml.polyline(points: points.map { |pt| pt.join(',') }.join(' ')) end # draw individual samples @samples.each do |sample| cx, cy = @chart.scaled_point(sample.input, sample.output) attributes = DefaultSampleAttributes.merge( title: sample.label, cx: cx, cy: cy, r: 3, ).merge(sample.attributes || {}) xml.circle(attributes) end xml.target! end |
#value_for(input) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/curvature/curve.rb', line 19 def value_for(input) unless @spliner @spliner = Spliner::Spliner.new( @samples.map(&:input), @samples.map(&:output), ) end @spliner[input] or raise "Can't find output for input #{input.inspect} on curve #{@name.inspect}" end |