|
| 1 | +module Quadtree |
| 2 | + # A Quadtree |
| 3 | + class Quadtree |
| 4 | + |
| 5 | + # Arbitrary constant to indicate how many elements can be stored in this |
| 6 | + # quad tree node. |
| 7 | + # @return [Integer] |
| 8 | + NODE_CAPACITY = 4 |
| 9 | + |
| 10 | + # Axis-aligned bounding box stored as a center with half-dimensions to |
| 11 | + # represent the boundaries of this quad tree. |
| 12 | + # @return [AxisAlignedBoundingBox] |
| 13 | + attr_accessor :boundary |
| 14 | + |
| 15 | + # Points in this quad tree node. |
| 16 | + # @return [Array<Point>] |
| 17 | + attr_accessor :points |
| 18 | + |
| 19 | + # Children |
| 20 | + |
| 21 | + # @return [Quadtree] |
| 22 | + attr_accessor :north_west |
| 23 | + # @return [Quadtree] |
| 24 | + attr_accessor :north_east |
| 25 | + # @return [Quadtree] |
| 26 | + attr_accessor :south_west |
| 27 | + # @return [Quadtree] |
| 28 | + attr_accessor :south_east |
| 29 | + |
| 30 | + def initialize(boundary) |
| 31 | + @boundary = boundary |
| 32 | + @points = [] |
| 33 | + @north_west = nil |
| 34 | + @north_east = nil |
| 35 | + @south_west = nil |
| 36 | + @south_east = nil |
| 37 | + end |
| 38 | + |
| 39 | + # @param [Point] point |
| 40 | + # @return [Boolean] |
| 41 | + def insert!(point) |
| 42 | + return false unless @boundary.contains_point?(point) |
| 43 | + |
| 44 | + if points.size < NODE_CAPACITY |
| 45 | + @points << point |
| 46 | + return true |
| 47 | + end |
| 48 | + |
| 49 | + subdivide! if @north_west.nil? |
| 50 | + return true if @north_west.insert(point) |
| 51 | + return true if @north_east.insert(point) |
| 52 | + return true if @south_west.insert(point) |
| 53 | + return true if @south_east.insert(point) |
| 54 | + |
| 55 | + false |
| 56 | + end |
| 57 | + |
| 58 | + # Finds all points contained within a range. |
| 59 | + # |
| 60 | + # @param [AxisAlignedBoundingBox] range the range to search within. |
| 61 | + # @return [Array<Point>] |
| 62 | + def query_range(range) |
| 63 | + # Prepare an array of results |
| 64 | + points_in_range = [] |
| 65 | + |
| 66 | + # Automatically abort if the range does not intersect this quad |
| 67 | + return points_in_range unless @boundary.intersects?(range) |
| 68 | + |
| 69 | + # Check objects at this quad level |
| 70 | + @points.each do |point| |
| 71 | + points_in_range << point if range.contains_point?(point) |
| 72 | + end |
| 73 | + |
| 74 | + # Terminate here, if there are no children |
| 75 | + return points_in_range if @north_west.nil? |
| 76 | + |
| 77 | + # Otherwise, add the points from the children |
| 78 | + points_in_range += @north_west.query_range(range) |
| 79 | + points_in_range += @north_east.query_range(range) |
| 80 | + points_in_range += @south_west.query_range(range) |
| 81 | + points_in_range += @south_east.query_range(range) |
| 82 | + |
| 83 | + points_in_range |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + # @return [Boolean] |
| 89 | + def subdivide! |
| 90 | + left_edge = @boundary.left |
| 91 | + right_edge = @boundary.right |
| 92 | + top_edge = @boundary.top |
| 93 | + bottom_edge = @boundary.bottom |
| 94 | + quad_half_dimension = @boundary.half_dimension / 2 |
| 95 | + |
| 96 | + north_west_center = Quadtree::Point.new left_edge + quad_half_dimension, top_edge - quad_half_dimension |
| 97 | + north_east_center = Quadtree::Point.new right_edge - quad_half_dimension, top_edge - quad_half_dimension |
| 98 | + south_east_center = Quadtree::Point.new left_edge + quad_half_dimension, bottom_edge + quad_half_dimension |
| 99 | + south_west_center = Quadtree::Point.new right_edge - quad_half_dimension, bottom_edge + quad_half_dimension |
| 100 | + |
| 101 | + north_west_boundary = Quadtree::AxisAlignedBoundingBox.new north_west_center, quad_half_dimension |
| 102 | + north_east_boundary = Quadtree::AxisAlignedBoundingBox.new north_east_center, quad_half_dimension |
| 103 | + south_west_boundary = Quadtree::AxisAlignedBoundingBox.new south_west_center, quad_half_dimension |
| 104 | + south_east_boundary = Quadtree::AxisAlignedBoundingBox.new south_east_center, quad_half_dimension |
| 105 | + |
| 106 | + @north_west = Quadtree::Quadtree.new north_west_boundary |
| 107 | + @north_east = Quadtree::Quadtree.new north_east_boundary |
| 108 | + @south_west = Quadtree::Quadtree.new south_west_boundary |
| 109 | + @south_east = Quadtree::Quadtree.new south_east_boundary |
| 110 | + |
| 111 | + true |
| 112 | + rescue |
| 113 | + false |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments