Class: CTioga2::Graphics::Types::Boundaries

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/types/boundaries.rb

Overview

An object representing boundaries for a plot.

todo Should be converted to using two SimpleRange objects. Will be more clear anyway.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right, top, bottom) ⇒ Boundaries

Creates a new Boundaries object with the given boundaries. A nil, false or NaN in one of those means unspecified.



136
137
138
139
140
141
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 136

def initialize(left, right, top, bottom)
  @left = left
  @right = right
  @top = top
  @bottom = bottom
end

Instance Attribute Details

#bottomObject

Boundaries



132
133
134
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 132

def bottom
  @bottom
end

#leftObject

Boundaries



132
133
134
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 132

def left
  @left
end

#rightObject

Boundaries



132
133
134
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 132

def right
  @right
end

#topObject

Boundaries



132
133
134
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 132

def top
  @top
end

Class Method Details

.bounds(x_values, y_values) ⇒ Object

Returns a boundary object that exactly contains all x_values and y_values (including error bars if applicable)



260
261
262
263
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 260

def self.bounds(x_values, y_values)
  return Boundaries.new(x_values.min, x_values.max,
                        y_values.max, y_values.min)
end

.from_ranges(horiz, vert) ⇒ Object

Creates a Boundaries object from two SimpleRange objects.



277
278
279
280
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 277

def self.from_ranges(horiz, vert)
  return Boundaries.new(horiz.first, horiz.last,
                        vert.last, vert.first)
end

.overall_bounds(bounds) ⇒ Object

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



268
269
270
271
272
273
274
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 268

def self.overall_bounds(bounds)
  retval = Boundaries.new(nil, nil, nil, nil)
  for b in bounds
    retval.extend(b)
  end
  return retval
end

Instance Method Details

#apply_margin!(margin) ⇒ Object

Apply a fixed margin on the Boundaries.



236
237
238
239
240
241
242
243
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 236

def apply_margin!(margin)
  w = self.width
  @left = @left - margin * w
  @right = @right + margin * w
  h = self.height
  @top = @top + margin * h
  @bottom = @bottom - margin * h
end

#extend(bounds) ⇒ Object

This function makes sures that the Boundaries object is big enough to encompass what it currently does and the bounds Boundaries object.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 199

def extend(bounds)
  # Left/right
  if (! @left.is_a? Float) or @left.nan? or
      (@left > bounds.left)
    @left = bounds.left
  end
  if (! @right.is_a? Float) or @right.nan? or
      (@right < bounds.right)
    @right = bounds.right
  end

  # Top/bottom
  if (! @top.is_a? Float) or @top.nan? or
      (@top < bounds.top)
    @top = bounds.top
  end
  if (! @bottom.is_a? Float) or @bottom.nan? or
      (@bottom > bounds.bottom)
    @bottom = bounds.bottom
  end
  return self
end

#extremaObject

Converts to an [xmin, xmax, ymin, ymax] array



182
183
184
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 182

def extrema
  return [xmin, xmax, ymin, ymax]
end

#heightObject

The algebraic height of the boundaries



192
193
194
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 192

def height
  return @top - @bottom
end

#horizontalObject

Returns a SimpleRange object corresponding to the horizontal range



170
171
172
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 170

def horizontal
  return SimpleRange.new(@left, @right)
end

#override_boundaries(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.



226
227
228
229
230
231
232
233
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 226

def override_boundaries(override)
  for el in [ :left, :right, :top, :bottom]
    val = override.send(el)
    if val and (val == val) # Strip NaN on the property that NaN != NaN
      self.send("#{el}=", val)
    end
  end
end

#set_from_range(range, which) ⇒ Object

Sets the values of the Boundaries for the which axis from the given range.



247
248
249
250
251
252
253
254
255
256
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 247

def set_from_range(range, which)
  case which
  when :x
    @left, @right = range.first, range.last
  when :y
    @bottom, @top = range.first, range.last
  else
    raise "What is this #{which} axis ? "
  end
end

#to_aObject

Converts to an array suitable for use with Tioga.



144
145
146
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 144

def to_a
  return [@left, @right, @top, @bottom]
end

#verticalObject

Returns a SimpleRange object corresponding to the vertical range



176
177
178
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 176

def vertical
  return SimpleRange.new(@bottom, @top)
end

#widthObject

The algebraic width of the boundaries



187
188
189
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 187

def width
  return @right - @left
end

#xmaxObject

Maximum x value



154
155
156
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 154

def xmax
  @left > @right ? @left : @right
end

#xminObject

Minimum x value



149
150
151
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 149

def xmin
  @left < @right ? @left : @right
end

#ymaxObject

Maxiumum y value



164
165
166
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 164

def ymax
  @bottom > @top ? @bottom : @top
end

#yminObject

Minimum y value



159
160
161
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 159

def ymin
  @bottom < @top ? @bottom : @top
end