Class: CTioga2::Graphics::Types::SimpleRange
- Inherits:
-
Object
- Object
- CTioga2::Graphics::Types::SimpleRange
- Defined in:
- lib/ctioga2/graphics/types/boundaries.rb
Overview
A range of coordinates.
Instance Attribute Summary collapse
-
#first ⇒ Object
Returns the value of attribute first.
-
#last ⇒ Object
Returns the value of attribute last.
Class Method Summary collapse
-
.bounds(values) ⇒ Object
Returns a SimpleRange object that is large enough to exactly contain all values.
-
.overall_range(ranges) ⇒ Object
Takes an array of Boundaries and returns a Boundaries object that precisely encompasses them all.
Instance Method Summary collapse
-
#apply_margin!(margin) ⇒ Object
Apply a fixed margin on the Boundaries.
-
#distance ⇒ Object
Algebraic distance.
-
#extend(range) ⇒ Object
This function makes sures that the SimpleRange object is big enough to encompass what it currently does and the range SimpleRange object.
-
#initialize(first, last = nil) ⇒ SimpleRange
constructor
Create a new SimpleRange object that runs from first to last (last can be less than first).
-
#max ⇒ Object
Maximum value.
-
#min ⇒ Object
Minimum value.
-
#override(override) ⇒ Object
Override the Boundaries with the contents of override.
Constructor Details
#initialize(first, last = nil) ⇒ SimpleRange
Create a new SimpleRange object that runs from first to last (last can be less than first). A nil, false or NaN in one of those means unspecified.
Alternatively, first can be an object that responds to #first and #last.
37 38 39 40 41 42 43 44 45 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 37 def initialize(first, last = nil) if first.respond_to?(:first) @first = first.first @last = first.last else @first = first @last = last end end |
Instance Attribute Details
#first ⇒ Object
Returns the value of attribute first.
29 30 31 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 29 def first @first end |
#last ⇒ Object
Returns the value of attribute last.
29 30 31 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 29 def last @last end |
Class Method Details
.bounds(values) ⇒ Object
Returns a SimpleRange object that is large enough to exactly contain all values
108 109 110 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 108 def self.bounds(values) return SimpleRange.new(values.min, values.max) end |
.overall_range(ranges) ⇒ Object
Takes an array of Boundaries and returns a Boundaries object that precisely encompasses them all. Invalid floats are simply ignored.
115 116 117 118 119 120 121 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 115 def self.overall_range(ranges) retval = SimpleRange.new(nil, nil) for r in ranges retval.extend(b) end return retval end |
Instance Method Details
#apply_margin!(margin) ⇒ Object
Apply a fixed margin on the Boundaries.
100 101 102 103 104 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 100 def apply_margin!(margin) d = self.distance @first = @first - margin * d @last = @last + margin * d end |
#distance ⇒ Object
Algebraic distance
58 59 60 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 58 def distance return @last - @first end |
#extend(range) ⇒ Object
This function makes sures that the SimpleRange object is big enough to encompass what it currently does and the range SimpleRange object.
todo this does not work correctly in the case of reversed boundaries. I don’t think it can anyway.
Actually, it even works with normal Range elements !
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 70 def extend(range) # Left/right if (! @first.is_a? Float) or @first.nan? or (range.first && @first > range.first) @first = range.first end if (! @last.is_a? Float) or @last.nan? or (range.last && @last < range.last) @last = range.last end return self end |
#max ⇒ Object
Maximum value
53 54 55 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 53 def max @first > @last ? @first : @last end |
#min ⇒ Object
Minimum value
48 49 50 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 48 def min @first < @last ? @first : @last end |
#override(override) ⇒ Object
Override the Boundaries with the contents of override. All elements which are not nil or NaN from override precisely override those in self.
90 91 92 93 94 95 96 97 |
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 90 def override(override) for el in [ :first, :last] val = override.send(el) if val and (val == val) # Strip NaN on the property that NaN != NaN self.send("#{el}=", val) end end end |