Skip to content

Commit 8136bd6

Browse files
committed
Merge branch 'release/v1.0.3'
2 parents eca886c + 584a60a commit 8136bd6

File tree

3 files changed

+57
-55
lines changed

3 files changed

+57
-55
lines changed

lib/quadtree/point.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ class Point
88
# @return [Float] Y coordinate
99
attr_accessor :y
1010

11-
# Payload attached to this {Point}.
11+
# @return [Object] Payload attached to this {Point}.
1212
attr_accessor :data
1313

1414
# Create a new {Point}.
1515
#
16-
# @param [Float] x X coordinate
17-
# @param [Float] y Y coordinate
16+
# @param [Float, Numeric] x X coordinate.
17+
# @param [Float, Numeric] y Y coordinate.
18+
# @param [Object] data {Point} payload (optional).
1819
def initialize(x, y, data=nil)
19-
@x = x
20-
@y = y
20+
@x = x.to_f
21+
@y = y.to_f
2122
@data = data unless data.nil?
2223
end
2324

@@ -48,17 +49,17 @@ def haversine_distance_to(other)
4849
# earth's radius
4950
r = 6371 * 1000.0
5051
# coverting degrees to radians
51-
lat1 = self.y * (Math::PI / 180)
52-
lat2 = other.y * (Math::PI / 180)
53-
dlat = (other.y - self.y) * (Math::PI / 180)
54-
dlon = (other.x - self.x) * (Math::PI / 180)
52+
lat1 = self.y * (Math::PI / 180.0)
53+
lat2 = other.y * (Math::PI / 180.0)
54+
dlat = (other.y - self.y) * (Math::PI / 180.0)
55+
dlon = (other.x - self.x) * (Math::PI / 180.0)
5556

5657
# a = sin²(Δφ/2) + cos φ_1 ⋅ cos φ_2 ⋅ sin²(Δλ/2)
57-
a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
58+
a = Math.sin(dlat / 2.0) * Math.sin(dlat / 2.0) +
5859
Math.cos(lat1) * Math.cos(lat2) *
59-
Math.sin(dlon / 2) * Math.sin(dlon / 2)
60+
Math.sin(dlon / 2.0) * Math.sin(dlon / 2.0)
6061
# c = 2 ⋅ atan2( √a, √(1−a) )
61-
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
62+
c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
6263
# d = R ⋅ c
6364
return r * c
6465
end

lib/quadtree/quadtree.rb

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/quadtree/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Quadtree
2-
VERSION = "1.0.2"
2+
VERSION = "1.0.3"
33
end

0 commit comments

Comments
 (0)