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.



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

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

Instance Attribute Details

#bottomObject

Boundaries



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

def bottom
  @bottom
end

#leftObject

Boundaries



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

def left
  @left
end

#rightObject

Boundaries



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

def right
  @right
end

#topObject

Boundaries



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

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)



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

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.



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

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.



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

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.



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

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.



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

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



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

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

#heightObject

The algebraic height of the boundaries



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

def height
  return @top - @bottom
end

#horizontalObject

Returns a SimpleRange object corresponding to the horizontal range



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

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.



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

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.



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

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.



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

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

#verticalObject

Returns a SimpleRange object corresponding to the vertical range



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

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

#widthObject

The algebraic width of the boundaries



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

def width
  return @right - @left
end

#xmaxObject

Maximum x value



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

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

#xminObject

Minimum x value



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

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

#ymaxObject

Maxiumum y value



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

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

#yminObject

Minimum y value



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

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