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).
Class Method Summary collapse
-
.from_json(json_data) ⇒ AxisAlignedBoundingBox
Construct a AxisAlignedBoundingBox from a JSON String.
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.
-
#to_h ⇒ Hash
Create a Hash for this AxisAlignedBoundingBox.
-
#to_hash ⇒ Hash
Create a Hash for this AxisAlignedBoundingBox.
-
#to_json(*_args) ⇒ String
Create a JSON String representation of this AxisAlignedBoundingBox.
-
#to_s ⇒ String
Create a String for this AxisAlignedBoundingBox.
-
#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.
19 20 21 22 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 19 def initialize(center, half_dimension) @center = center @half_dimension = half_dimension.to_f end |
Instance Attribute Details
#center ⇒ Point
Center Point of this instance.
9 10 11 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 9 def center @center end |
#half_dimension ⇒ Float
Half dimension of this instance (distance from the center Point to the edge).
15 16 17 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 15 def half_dimension @half_dimension end |
Class Method Details
.from_json(json_data) ⇒ AxisAlignedBoundingBox
Construct a Quadtree::AxisAlignedBoundingBox from a JSON String.
71 72 73 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 71 def self.from_json(json_data) new(Point.from_json(json_data['center']), json_data['half_dimension']) end |
Instance Method Details
#bottom ⇒ Float
Get the Y coordinate of the bottom edge of this instance.
131 132 133 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 131 def bottom @center.y - @half_dimension end |
#contains_point?(point) ⇒ Boolean
Check if this instance contains a given Point.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 80 def contains_point?(point) if point.x >= center.x - half_dimension && point.x <= center.x + half_dimension if point.y >= center.y - half_dimension && point.y <= center.y + half_dimension return true end end false end |
#height ⇒ Float
Get the height of this instance.
145 146 147 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 145 def height span end |
#intersects?(other) ⇒ Boolean
Check if this instance intersects with another instance.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 95 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 contains_point?(corner) end false end |
#left ⇒ Float
Get the X coordinate of the left edge of this instance.
110 111 112 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 110 def left @center.x - @half_dimension end |
#right ⇒ Float
Get the X coordinate of the right edge of this instance.
117 118 119 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 117 def right @center.x + @half_dimension end |
#to_h ⇒ Hash
Create a Hash for this Quadtree::AxisAlignedBoundingBox.
29 30 31 32 33 34 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 29 def to_h { 'center': center.to_h, 'half_dimension': half_dimension } end |
#to_hash ⇒ Hash
Create a Hash for this Quadtree::AxisAlignedBoundingBox.
41 42 43 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 41 def to_hash to_h end |
#to_json(*_args) ⇒ String
Create a JSON String representation of this Quadtree::AxisAlignedBoundingBox.
50 51 52 53 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 50 def to_json(*_args) require 'json' to_h.to_json end |
#to_s ⇒ String
Create a String for this Quadtree::AxisAlignedBoundingBox.
60 61 62 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 60 def to_s to_h.to_s end |
#top ⇒ Float
Get the Y coordinate of the top edge of this instance.
124 125 126 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 124 def top @center.y + @half_dimension end |
#width ⇒ Float
Get the width of this instance.
138 139 140 |
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 138 def width span end |