class AxisAlignedBoundingBox

Attributes

center[R]
half_dimension[R]

Public Class Methods

new(center, half_dimension) click to toggle source
# File lib/misc/axis_aligned_bounding_box.rb, line 3
def initialize(center, half_dimension)
  @center = center
  @half_dimension = half_dimension
  @dhx = (@half_dimension[0] - @center[0]).abs
  @dhy = (@half_dimension[1] - @center[1]).abs
end

Public Instance Methods

contains?(point) click to toggle source
# File lib/misc/axis_aligned_bounding_box.rb, line 10
def contains?(point)
  return false unless (@center[0] + @dhx) >= point[0]
  return false unless (@center[0] - @dhx) <= point[0]
  return false unless (@center[1] + @dhy) >= point[1]
  return false unless (@center[1] - @dhy) <= point[1]
  true
end
intersects?(other) click to toggle source
# File lib/misc/axis_aligned_bounding_box.rb, line 18
def intersects?(other)
  ocx, ocy = other.center
  ohx, ohy = other.half_dimension
  odhx = (ohx - ocx).abs
  return false unless (@center[0] + @dhx) >= (ocx - odhx)
  return false unless (@center[0] - @dhx) <= (ocx + odhx)
  odhy = (ohy - ocy).abs
  return false unless (@center[1] + @dhy) >= (ocy - odhy)
  return false unless (@center[1] - @dhy) <= (ocy + odhy)
  true
end
to_s() click to toggle source
# File lib/misc/axis_aligned_bounding_box.rb, line 30
def to_s
  "c: #{@center}, h: #{@half_dimension}"
end