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.



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

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

Instance Attribute Details

#bottomObject

Boundaries



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

def bottom
  @bottom
end

#leftObject

Boundaries



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

def left
  @left
end

#rightObject

Boundaries



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

def right
  @right
end

#topObject

Boundaries



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

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)



267
268
269
270
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 267

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.



284
285
286
287
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 284

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.



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

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.



243
244
245
246
247
248
249
250
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 243

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.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 206

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



189
190
191
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 189

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

#heightObject

The algebraic height of the boundaries



199
200
201
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 199

def height
  return @top - @bottom
end

#horizontalObject

Returns a SimpleRange object corresponding to the horizontal range



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

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.



233
234
235
236
237
238
239
240
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 233

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.



254
255
256
257
258
259
260
261
262
263
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 254

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.



151
152
153
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 151

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

#verticalObject

Returns a SimpleRange object corresponding to the vertical range



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

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

#widthObject

The algebraic width of the boundaries



194
195
196
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 194

def width
  return @right - @left
end

#xmaxObject

Maximum x value



161
162
163
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 161

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

#xminObject

Minimum x value



156
157
158
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 156

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

#ymaxObject

Maxiumum y value



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

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

#yminObject

Minimum y value



166
167
168
# File 'lib/ctioga2/graphics/types/boundaries.rb', line 166

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