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.

Since:

  • 1.0.0

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)

Since:

  • 1.0.0



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

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

Instance Attribute Details

#centerPoint

Returns:

Since:

  • 1.0.0



6
7
8
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 6

def center
  @center
end

#half_dimensionFloat

Returns:

  • (Float)

Since:

  • 1.0.0



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

def half_dimension
  @half_dimension
end

Instance Method Details

#bottomFloat

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

Returns:

  • (Float)

    the Y coordinate of the bottom edge of this AABB.

Since:

  • 1.0.0



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

def bottom
  @center.y - @half_dimension
end

#contains_point?(point) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

Since:

  • 1.0.0



19
20
21
22
23
24
25
26
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 19

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

#heightFloat

Returns:

  • (Float)

Since:

  • 1.0.0



72
73
74
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 72

def height
  span
end

#intersects?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

Since:

  • 1.0.0



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 30

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

Returns:

  • (Float)

Since:

  • 1.0.0



43
44
45
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 43

def left
  @center.x - @half_dimension
end

#rightFloat

Returns:

  • (Float)

Since:

  • 1.0.0



48
49
50
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 48

def right
  @center.x + @half_dimension
end

#topFloat

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

Returns:

  • (Float)

    the Y coordinate of the top edge of this AABB.

Since:

  • 1.0.0



55
56
57
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 55

def top
  @center.y + @half_dimension
end

#widthFloat

Returns:

  • (Float)

Since:

  • 1.0.0



67
68
69
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 67

def width
  span
end