@@ -28,29 +28,29 @@ class Quadtree
2828 attr_accessor :south_east
2929
3030 def initialize ( boundary )
31- @ boundary = boundary
32- @ points = [ ]
33- @ north_west = nil
34- @ north_east = nil
35- @ south_west = nil
36- @ south_east = nil
31+ self . boundary = boundary
32+ self . points = [ ]
33+ self . north_west = nil
34+ self . north_east = nil
35+ self . south_west = nil
36+ self . south_east = nil
3737 end
3838
3939 # @param [Point] point
4040 # @return [Boolean]
4141 def insert! ( point )
42- return false unless @ boundary. contains_point? ( point )
42+ return false unless self . boundary . contains_point? ( point )
4343
44- if points . size < NODE_CAPACITY
45- @ points << point
44+ if self . points . size < NODE_CAPACITY
45+ self . points << point
4646 return true
4747 end
4848
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 )
49+ subdivide! if self . north_west . nil?
50+ return true if self . north_west . insert! ( point )
51+ return true if self . north_east . insert! ( point )
52+ return true if self . south_west . insert! ( point )
53+ return true if self . south_east . insert! ( point )
5454
5555 false
5656 end
@@ -64,21 +64,21 @@ def query_range(range)
6464 points_in_range = [ ]
6565
6666 # Automatically abort if the range does not intersect this quad
67- return points_in_range unless @ boundary. intersects? ( range )
67+ return points_in_range unless self . boundary . intersects? ( range )
6868
6969 # Check objects at this quad level
70- @ points. each do |point |
70+ self . points . each do |point |
7171 points_in_range << point if range . contains_point? ( point )
7272 end
7373
7474 # Terminate here, if there are no children
75- return points_in_range if @ north_west. nil?
75+ return points_in_range if self . north_west . nil?
7676
7777 # 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 )
78+ points_in_range += self . north_west . query_range ( range )
79+ points_in_range += self . north_east . query_range ( range )
80+ points_in_range += self . south_west . query_range ( range )
81+ points_in_range += self . south_east . query_range ( range )
8282
8383 points_in_range
8484 end
@@ -87,29 +87,30 @@ def query_range(range)
8787
8888 # @return [Boolean]
8989 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
90+ left_edge = self . boundary . left
91+ right_edge = self . boundary . right
92+ top_edge = self . boundary . top
93+ bottom_edge = self . boundary . bottom
94+ quad_half_dimension = self . boundary . half_dimension / 2
95+
96+ north_west_center = Point . new left_edge + quad_half_dimension , top_edge - quad_half_dimension
97+ north_east_center = Point . new right_edge - quad_half_dimension , top_edge - quad_half_dimension
98+ south_east_center = Point . new left_edge + quad_half_dimension , bottom_edge + quad_half_dimension
99+ south_west_center = Point . new right_edge - quad_half_dimension , bottom_edge + quad_half_dimension
100+
101+ north_west_boundary = AxisAlignedBoundingBox . new north_west_center , quad_half_dimension
102+ north_east_boundary = AxisAlignedBoundingBox . new north_east_center , quad_half_dimension
103+ south_west_boundary = AxisAlignedBoundingBox . new south_west_center , quad_half_dimension
104+ south_east_boundary = AxisAlignedBoundingBox . new south_east_center , quad_half_dimension
105+
106+ self . north_west = Quadtree . new north_west_boundary
107+ self . north_east = Quadtree . new north_east_boundary
108+ self . south_west = Quadtree . new south_west_boundary
109+ self . south_east = Quadtree . new south_east_boundary
110110
111111 true
112- rescue
112+ rescue => error
113+ puts "Something went wrong: #{ error } "
113114 false
114115 end
115116 end
0 commit comments