class CTioga2::Graphics::Types::SimpleRange

A range of coordinates.

Attributes

first[RW]
last[RW]

Public Class Methods

bounds(values) click to toggle source

Returns a SimpleRange object that is large enough to exactly contain all values

# File lib/ctioga2/graphics/types/boundaries.rb, line 125
def self.bounds(values)
  return SimpleRange.new(values.min, values.max)
end
new(first, last = nil) click to toggle source

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.

# 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
overall_range(ranges) click to toggle source

Takes an array of Boundaries and returns a Boundaries object that precisely encompasses them all. Invalid floats are simply ignored.

# File lib/ctioga2/graphics/types/boundaries.rb, line 132
def self.overall_range(ranges)
  retval = SimpleRange.new(nil, nil)
  for r in ranges
    retval.extend(b)
  end
  return retval
end

Public Instance Methods

apply_margin!(margin) click to toggle source

Apply a fixed margin on the Boundaries.

# File lib/ctioga2/graphics/types/boundaries.rb, line 117
def apply_margin!(margin)
  d = self.distance
  @first = @first - margin * d
  @last = @last + margin * d
end
distance() click to toggle source

Algebraic distance

# File lib/ctioga2/graphics/types/boundaries.rb, line 75
def distance
  return @last - @first
end
extend(range) click to toggle source

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 !

# File lib/ctioga2/graphics/types/boundaries.rb, line 87
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
infinite?() click to toggle source
# File lib/ctioga2/graphics/types/boundaries.rb, line 59
def infinite?
  return (Utils::infinite_number?(@first) or
          Utils::infinite_number?(@last))
end
max() click to toggle source

Maximum value

# File lib/ctioga2/graphics/types/boundaries.rb, line 70
def max
  @first > @last ? @first : @last
end
min() click to toggle source

Minimum value

# File lib/ctioga2/graphics/types/boundaries.rb, line 65
def min
  @first < @last ? @first : @last
end
nan?() click to toggle source
# File lib/ctioga2/graphics/types/boundaries.rb, line 54
def nan?
  return (Utils::nan_number?(@first) or
          Utils::nan_number?(@last))
end
override(override) click to toggle source

Override the Boundaries with the contents of override. All elements which are not nil or NaN from override precisely override those in self.

# File lib/ctioga2/graphics/types/boundaries.rb, line 107
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
valid?() click to toggle source

Checks if the range is valid, that is both elements are finite numbers

# File lib/ctioga2/graphics/types/boundaries.rb, line 49
def valid?
  return (Utils::finite_number?(@first) and
          Utils::finite_number?(@last))
end