Class: Quadtree::AxisAlignedBoundingBox

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

Overview

Axis-aligned bounding box with half dimension and center.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, half_dimension) ⇒ AxisAlignedBoundingBox

Returns a new instance of AxisAlignedBoundingBox.

Parameters:

  • center (Point)
  • half_dimension (Float)


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

#centerPoint

Center Point of this instance.

Returns:

  • (Point)

    the Point marking the center of this instance.



9
10
11
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 9

def center
  @center
end

#half_dimensionFloat

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

Returns:

  • (Float)

    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.

Parameters:

  • json_data (String)

    input JSON String.

Returns:



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

#bottomFloat

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

Returns:

  • (Float)

    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.

Parameters:

Returns:

  • (Boolean)

    true if given Point is contained, false otherwise.



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

#heightFloat

Get the height of this instance.

Returns:

  • (Float)


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.

Parameters:

Returns:

  • (Boolean)

    true if these intersects, false otherwise.



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

#leftFloat

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

Returns:

  • (Float)

    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

#rightFloat

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

Returns:

  • (Float)

    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_hHash

Create a Hash for this Quadtree::AxisAlignedBoundingBox.

Returns:



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_hashHash

Create a Hash for this Quadtree::AxisAlignedBoundingBox.

Returns:



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.

Returns:



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_sString

Create a String for this Quadtree::AxisAlignedBoundingBox.

Returns:



60
61
62
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 60

def to_s
  to_h.to_s
end

#topFloat

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

Returns:

  • (Float)

    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

#widthFloat

Get the width of this instance.

Returns:

  • (Float)


138
139
140
# File 'lib/quadtree/axis_aligned_bounding_box.rb', line 138

def width
  span
end