Class: AxisAlignedBoundingBox
- Inherits:
-
Object
- Object
- AxisAlignedBoundingBox
- Defined in:
- lib/misc/axis_aligned_bounding_box.rb
Instance Attribute Summary collapse
-
#center ⇒ Object
readonly
Returns the value of attribute center.
-
#half_dimension ⇒ Object
readonly
Returns the value of attribute half_dimension.
Instance Method Summary collapse
- #contains?(point) ⇒ Boolean
-
#initialize(center, half_dimension) ⇒ AxisAlignedBoundingBox
constructor
A new instance of AxisAlignedBoundingBox.
- #intersects?(other) ⇒ Boolean
- #to_s ⇒ Object
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
#center ⇒ Object (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_dimension ⇒ Object (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
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
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_s ⇒ Object
30 31 32 |
# File 'lib/misc/axis_aligned_bounding_box.rb', line 30 def to_s "c: #{@center}, h: #{@half_dimension}" end |