Class: Quadtree::AxisAlignedBoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtree/axis_aligned_bounding_box.rb

Overview

Axis-aligned bounding box with half dimension and center.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, half_dimension) ⇒ AxisAlignedBoundingBox

Returns a new instance of AxisAlignedBoundingBox.

Parameters:

  • center (Point)
  • half_dimension (Float)


18
19
20
21
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 18

def initialize(center, half_dimension)
  @center = center
  @half_dimension = half_dimension.to_f
end

Instance Attribute Details

#centerPoint

Center Point of this instance.

Returns:

  • (Point)

    the Point marking the center of this instance.



8
9
10
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 8

def center
  @center
end

#half_dimensionFloat

Half dimension of this instance (distance from the center Point to the edge).

Returns:

  • (Float)

    distance from the center Point to the edge.



14
15
16
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 14

def half_dimension
  @half_dimension
end

Instance Method Details

#bottomFloat

Get the Y coordinate of the bottom edge of this instance.

Returns:

  • (Float)

    the Y coordinate of the bottom edge of this instance.



84
85
86
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 84

def bottom
  @center.y - @half_dimension
end

#contains_point?(point) ⇒ Boolean

Check if this instance contains a given Point.

Parameters:

Returns:

  • (Boolean)

    true if given Point is contained, false otherwise.



28
29
30
31
32
33
34
35
36
37
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 28

def contains_point?(point)
  if point.x >= self.center.x - self.half_dimension &&
      point.x <= self.center.x + self.half_dimension
    if point.y >= self.center.y - self.half_dimension &&
      point.y <= self.center.y + self.half_dimension
      return true
    end
  end
  false
end

#heightFloat

Get the height of this instance.

Returns:

  • (Float)


98
99
100
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 98

def height
  span
end

#intersects?(other) ⇒ Boolean

Check if this instance intersects with another instance.

Parameters:

Returns:

  • (Boolean)

    true if these intersects, false otherwise.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 43

def intersects?(other)
  other_lt_corner = Point.new(other.left, other.top)
  other_rt_corner = Point.new(other.right, other.top)
  other_lb_corner = Point.new(other.left, other.bottom)
  other_rb_corner = Point.new(other.right, other.bottom)

  [
    other_lt_corner,
    other_rt_corner,
    other_lb_corner,
    other_rb_corner
  ].each do |corner|
    return true if self.contains_point?(corner)
  end
  false
end

#leftFloat

Get the X coordinate of the left edge of this instance.

Returns:

  • (Float)

    the X coordinate of the left edge of this instance.



63
64
65
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 63

def left
  @center.x - @half_dimension
end

#rightFloat

Get the X coordinate of the right edge of this instance.

Returns:

  • (Float)

    the X coordinate of the right edge of this instance.



70
71
72
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 70

def right
  @center.x + @half_dimension
end

#topFloat

Get the Y coordinate of the top edge of this instance.

Returns:

  • (Float)

    the Y coordinate of the top edge of this instance.



77
78
79
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 77

def top
  @center.y + @half_dimension
end

#widthFloat

Get the width of this instance.

Returns:

  • (Float)


91
92
93
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 91

def width
  span
end