Class: AxisAlignedBoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/misc/axis_aligned_bounding_box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, half_dimension) ⇒ AxisAlignedBoundingBox

Returns a new instance of AxisAlignedBoundingBox.



3
4
5
6
7
8
# 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

Instance Attribute Details

#centerObject (readonly)

Returns the value of attribute center.



2
3
4
# File 'lib/misc/axis_aligned_bounding_box.rb', line 2

def center
  @center
end

#half_dimensionObject (readonly)

Returns the value of attribute half_dimension.



2
3
4
# File 'lib/misc/axis_aligned_bounding_box.rb', line 2

def half_dimension
  @half_dimension
end

Instance Method Details

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# 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) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
# 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_sObject



30
31
32
# File 'lib/misc/axis_aligned_bounding_box.rb', line 30

def to_s
  "c: #{@center}, h: #{@half_dimension}"
end