Class: Quadtree::AxisAlignedBoundingBox
- Inherits:
-
Object
- Object
- Quadtree::AxisAlignedBoundingBox
- Defined in:
- lib/quadtree/axis_aligned_bounding_box.rb
Overview
Axis-aligned bounding box with half dimension and center.
Instance Attribute Summary collapse
-
#center ⇒ Point
Center Point of this instance.
-
#half_dimension ⇒ Float
Half dimension of this instance (distance from the center Point to the edge).
Instance Method Summary collapse
-
#bottom ⇒ Float
Get the Y coordinate of the bottom edge of this instance.
-
#contains_point?(point) ⇒ Boolean
Check if this instance contains a given Point.
-
#height ⇒ Float
Get the height of this instance.
-
#initialize(center, half_dimension) ⇒ AxisAlignedBoundingBox
constructor
A new instance of AxisAlignedBoundingBox.
-
#intersects?(other) ⇒ Boolean
Check if this instance intersects with another instance.
-
#left ⇒ Float
Get the X coordinate of the left edge of this instance.
-
#right ⇒ Float
Get the X coordinate of the right edge of this instance.
-
#top ⇒ Float
Get the Y coordinate of the top edge of this instance.
-
#width ⇒ Float
Get the width of this instance.
Constructor Details
#initialize(center, half_dimension) ⇒ AxisAlignedBoundingBox
Returns a new instance of AxisAlignedBoundingBox.
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
#center ⇒ Point
Center Point of this instance.
8 9 10 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 8 def center @center end |
#half_dimension ⇒ Float
Half dimension of this instance (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
#bottom ⇒ Float
Get 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.
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 |
#height ⇒ Float
Get the height of this instance.
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.
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 |
#left ⇒ Float
Get 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 |
#right ⇒ Float
Get 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 |
#top ⇒ Float
Get 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 |
#width ⇒ Float
Get the width of this instance.
91 92 93 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 91 def width span end |