class Quadtree::AxisAlignedBoundingBox

Axis-aligned bounding box with half dimension and center.

Attributes

center[RW]

Center {Point} of this instance.

@return [Point] the {Point} marking the center of this instance.

half_dimension[RW]

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

@return [Float] distance from the center {Point} to the edge.

Public Class Methods

from_json(json_data) click to toggle source

Construct a {AxisAlignedBoundingBox} from a JSON String.

@param [String] json_data input JSON String.

@return [AxisAlignedBoundingBox] the {AxisAlignedBoundingBox} contained in the JSON String.

# 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
new(center, half_dimension) click to toggle source

@param center [Point] @param half_dimension [Float]

# File lib/quadtree/axis_aligned_bounding_box.rb, line 19
def initialize(center, half_dimension)
  @center = center
  @half_dimension = half_dimension.to_f
end

Public Instance Methods

bottom() click to toggle source

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

@return [Float] the Y coordinate of the bottom edge of this instance.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 131
def bottom
  @center.y - @half_dimension
end
contains_point?(point) click to toggle source

Check if this instance contains a given {Point}.

@param point [Point] the {Point} to check for. @return [Boolean] true if given {Point} is contained, false

otherwise.
# 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() click to toggle source

Get the height of this instance.

@return [Float]

# File lib/quadtree/axis_aligned_bounding_box.rb, line 145
def height
  span
end
intersects?(other) click to toggle source

Check if this instance intersects with another instance.

@param other [AxisAlignedBoundingBox] the other instance to check for. @return [Boolean] true if these intersects, false otherwise.

# 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() click to toggle source

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

@return [Float] the X coordinate of the left edge of this instance.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 110
def left
  @center.x - @half_dimension
end
right() click to toggle source

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

@return [Float] the X coordinate of the right edge of this instance.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 117
def right
  @center.x + @half_dimension
end
to_h() click to toggle source

Create a Hash for this {AxisAlignedBoundingBox}.

@return [Hash] Hash representation of this {AxisAlignedBoundingBox}.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 29
def to_h
  {
    'center': center.to_h,
    'half_dimension': half_dimension
  }
end
to_hash() click to toggle source

Create a Hash for this {AxisAlignedBoundingBox}.

@return [Hash] Hash representation of this {AxisAlignedBoundingBox}.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 41
def to_hash
  to_h
end
to_json(*_args) click to toggle source

Create a JSON String representation of this {AxisAlignedBoundingBox}.

@return [String] JSON String of this {AxisAlignedBoundingBox}.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 50
def to_json(*_args)
  require 'json'
  to_h.to_json
end
to_s() click to toggle source

Create a String for this {AxisAlignedBoundingBox}.

@return [String] String representation of this {AxisAlignedBoundingBox}.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 60
def to_s
  to_h.to_s
end
top() click to toggle source

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

@return [Float] the Y coordinate of the top edge of this instance.

# File lib/quadtree/axis_aligned_bounding_box.rb, line 124
def top
  @center.y + @half_dimension
end
width() click to toggle source

Get the width of this instance.

@return [Float]

# File lib/quadtree/axis_aligned_bounding_box.rb, line 138
def width
  span
end

Private Instance Methods

span() click to toggle source
# File lib/quadtree/axis_aligned_bounding_box.rb, line 151
def span
  @half_dimension * 2
end