Class: Geom2D::BoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/geom2d/bounding_box.rb

Overview

Represents an axis aligned bounding box.

An empty bounding box contains just the point at origin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_x = 0, min_y = 0, max_x = 0, max_y = 0) ⇒ BoundingBox

Creates a new BoundingBox.



31
32
33
34
35
36
# File 'lib/geom2d/bounding_box.rb', line 31

def initialize(min_x = 0, min_y = 0, max_x = 0, max_y = 0)
  @min_x = min_x
  @min_y = min_y
  @max_x = max_x
  @max_y = max_y
end

Instance Attribute Details

#max_xObject (readonly)

The maximum x-coordinate.



25
26
27
# File 'lib/geom2d/bounding_box.rb', line 25

def max_x
  @max_x
end

#max_yObject (readonly)

The maximum y-coordinate.



28
29
30
# File 'lib/geom2d/bounding_box.rb', line 28

def max_y
  @max_y
end

#min_xObject (readonly)

The minimum x-coordinate.



19
20
21
# File 'lib/geom2d/bounding_box.rb', line 19

def min_x
  @min_x
end

#min_yObject (readonly)

The minimum y-coordinate.



22
23
24
# File 'lib/geom2d/bounding_box.rb', line 22

def min_y
  @min_y
end

Instance Method Details

#add(other) ⇒ Object Also known as: +

Returns a bounding box containing this bounding box and the argument.



68
69
70
# File 'lib/geom2d/bounding_box.rb', line 68

def add(other)
  dup.add!(other)
end

#add!(other) ⇒ Object

Updates this bounding box to also contain the given bounding box or point.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/geom2d/bounding_box.rb', line 39

def add!(other)
  case other
  when BoundingBox
    @min_x = [min_x, other.min_x].min
    @min_y = [min_y, other.min_y].min
    @max_x = [max_x, other.max_x].max
    @max_y = [max_y, other.max_y].max
  when Point
    @min_x = [min_x, other.x].min
    @min_y = [min_y, other.y].min
    @max_x = [max_x, other.x].max
    @max_y = [max_y, other.y].max
  else
    raise ArgumentError, "Can only use another BoundingBox or Point"
  end
  self
end

#heightObject

Returns the height of the bounding box.



63
64
65
# File 'lib/geom2d/bounding_box.rb', line 63

def height
  @max_y - @min_y
end

#inspectObject

:nodoc:



78
79
80
# File 'lib/geom2d/bounding_box.rb', line 78

def inspect # :nodoc:
  "BBox[#{min_x}, #{min_y}, #{max_x}, #{max_y}]"
end

#to_aObject

Returns the bounding box as an array of the form [min_x, min_y, max_x, max_y].



74
75
76
# File 'lib/geom2d/bounding_box.rb', line 74

def to_a
  [@min_x, @min_y, @max_x, @max_y]
end

#widthObject

Returns the width of the bounding box.



58
59
60
# File 'lib/geom2d/bounding_box.rb', line 58

def width
  @max_x - @min_x
end