quadtree 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/bitbucket-pipelines.yml +3 -3
- data/lib/quadtree/axis_aligned_bounding_box.rb +43 -17
- data/lib/quadtree/point.rb +18 -14
- data/lib/quadtree/quadtree.rb +15 -5
- data/lib/quadtree/version.rb +2 -1
- data/lib/quadtree.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e77411d8a1691d1187b9fe70bd783838b2b29c9f462445c7c00535a97f1f1647
|
4
|
+
data.tar.gz: 46b84bb36f80750c9f20688532860e54bf573ba1d2f72a5687d093a8f17384bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6411caa3b61a5b9c3f2e5ca06c15301334957d3ccfb42fb7d7b0eabc22f1edf1e0e4f60a108265ee647af9084c6645c87e0b89c11cd8194964f8aed2857eebf0
|
7
|
+
data.tar.gz: 2eba1b1e00508e045030ebda1dc2d49f691fa73c674e4f736f8d0d69e0189cfd1eee079797822c6c2d3c3913a29bb5c0f34d76194de21c26f591660e3064ec68
|
data/README.md
CHANGED
data/bitbucket-pipelines.yml
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# This is a sample build configuration for Ruby.
|
2
|
-
# Check our guides at https://confluence.atlassian.com/x/8r-5Mw for more examples.
|
3
1
|
image: ruby:2.4.0
|
4
2
|
|
5
3
|
pipelines:
|
@@ -9,8 +7,10 @@ pipelines:
|
|
9
7
|
caches:
|
10
8
|
- bundler
|
11
9
|
script:
|
12
|
-
- bundle install
|
10
|
+
- bundle install --path ./vendor
|
11
|
+
- bundle exec rake build
|
13
12
|
- bundle exec rake spec
|
13
|
+
- bundle exec rake yard
|
14
14
|
|
15
15
|
definitions:
|
16
16
|
caches:
|
@@ -2,72 +2,98 @@ module Quadtree
|
|
2
2
|
# Axis-aligned bounding box with half dimension and center.
|
3
3
|
class AxisAlignedBoundingBox
|
4
4
|
|
5
|
-
#
|
5
|
+
# Center {Point} of this instance.
|
6
|
+
#
|
7
|
+
# @return [Point] the {Point} marking the center of this instance.
|
6
8
|
attr_accessor :center
|
7
|
-
|
9
|
+
|
10
|
+
# Half dimension of this instance (distance from the center {Point} to the
|
11
|
+
# edge).
|
12
|
+
#
|
13
|
+
# @return [Float] distance from the center {Point} to the edge.
|
8
14
|
attr_accessor :half_dimension
|
9
15
|
|
10
|
-
# @param [Point]
|
11
|
-
# @param [Float]
|
16
|
+
# @param center [Point]
|
17
|
+
# @param half_dimension [Float]
|
12
18
|
def initialize(center, half_dimension)
|
13
19
|
@center = center
|
14
20
|
@half_dimension = half_dimension.to_f
|
15
21
|
end
|
16
22
|
|
17
|
-
#
|
18
|
-
#
|
23
|
+
# Check if this instance contains a given {Point}.
|
24
|
+
#
|
25
|
+
# @param point [Point] the {Point} to check for.
|
26
|
+
# @return [Boolean] +true+ if given {Point} is contained, +false+
|
27
|
+
# otherwise.
|
19
28
|
def contains_point?(point)
|
20
|
-
if point.x >= self.center.x - self.half_dimension
|
21
|
-
|
29
|
+
if point.x >= self.center.x - self.half_dimension &&
|
30
|
+
point.x <= self.center.x + self.half_dimension
|
31
|
+
if point.y >= self.center.y - self.half_dimension &&
|
32
|
+
point.y <= self.center.y + self.half_dimension
|
22
33
|
return true
|
23
34
|
end
|
24
35
|
end
|
25
36
|
false
|
26
37
|
end
|
27
38
|
|
28
|
-
#
|
29
|
-
#
|
39
|
+
# Check if this instance intersects with another instance.
|
40
|
+
#
|
41
|
+
# @param other [AxisAlignedBoundingBox] the other instance to check for.
|
42
|
+
# @return [Boolean] +true+ if these intersects, +false+ otherwise.
|
30
43
|
def intersects?(other)
|
31
44
|
other_lt_corner = Point.new(other.left, other.top)
|
32
45
|
other_rt_corner = Point.new(other.right, other.top)
|
33
46
|
other_lb_corner = Point.new(other.left, other.bottom)
|
34
47
|
other_rb_corner = Point.new(other.right, other.bottom)
|
35
48
|
|
36
|
-
[
|
49
|
+
[
|
50
|
+
other_lt_corner,
|
51
|
+
other_rt_corner,
|
52
|
+
other_lb_corner,
|
53
|
+
other_rb_corner
|
54
|
+
].each do |corner|
|
37
55
|
return true if self.contains_point?(corner)
|
38
56
|
end
|
39
57
|
false
|
40
58
|
end
|
41
59
|
|
42
|
-
#
|
60
|
+
# Get the X coordinate of the left edge of this instance.
|
61
|
+
#
|
62
|
+
# @return [Float] the X coordinate of the left edge of this instance.
|
43
63
|
def left
|
44
64
|
@center.x - @half_dimension
|
45
65
|
end
|
46
66
|
|
47
|
-
#
|
67
|
+
# Get the X coordinate of the right edge of this instance.
|
68
|
+
#
|
69
|
+
# @return [Float] the X coordinate of the right edge of this instance.
|
48
70
|
def right
|
49
71
|
@center.x + @half_dimension
|
50
72
|
end
|
51
73
|
|
52
|
-
# Get the Y coordinate of the top edge of this
|
74
|
+
# Get the Y coordinate of the top edge of this instance.
|
53
75
|
#
|
54
|
-
# @return [Float] the Y coordinate of the top edge of this
|
76
|
+
# @return [Float] the Y coordinate of the top edge of this instance.
|
55
77
|
def top
|
56
78
|
@center.y + @half_dimension
|
57
79
|
end
|
58
80
|
|
59
|
-
# Get the Y coordinate of the bottom edge of this
|
81
|
+
# Get the Y coordinate of the bottom edge of this instance.
|
60
82
|
#
|
61
|
-
# @return [Float] the Y coordinate of the bottom edge of this
|
83
|
+
# @return [Float] the Y coordinate of the bottom edge of this instance.
|
62
84
|
def bottom
|
63
85
|
@center.y - @half_dimension
|
64
86
|
end
|
65
87
|
|
88
|
+
# Get the width of this instance.
|
89
|
+
#
|
66
90
|
# @return [Float]
|
67
91
|
def width
|
68
92
|
span
|
69
93
|
end
|
70
94
|
|
95
|
+
# Get the height of this instance.
|
96
|
+
#
|
71
97
|
# @return [Float]
|
72
98
|
def height
|
73
99
|
span
|
data/lib/quadtree/point.rb
CHANGED
@@ -2,48 +2,52 @@ module Quadtree
|
|
2
2
|
# Simple coordinate object to represent points in some space.
|
3
3
|
class Point
|
4
4
|
|
5
|
-
#
|
5
|
+
# The X coordinate of this instance.
|
6
|
+
# @return [Float] X coordinate.
|
6
7
|
attr_accessor :x
|
7
8
|
|
8
|
-
#
|
9
|
+
# The Y coordinate of this instance.
|
10
|
+
# @return [Float] Y coordinate.
|
9
11
|
attr_accessor :y
|
10
12
|
|
11
|
-
#
|
13
|
+
# Optional payload attached to this instance.
|
14
|
+
# @return [Object] payload attached to this instance.
|
12
15
|
attr_accessor :data
|
13
16
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# @param [
|
17
|
-
#
|
18
|
-
# @param [Object] data {Point} payload (optional).
|
17
|
+
# @param x [Float, Numeric] X coordinate.
|
18
|
+
# @param y [Float, Numeric] Y coordinate.
|
19
|
+
# @param data [Object] payload payload attached to this instance
|
20
|
+
# (optional).
|
19
21
|
def initialize(x, y, data=nil)
|
20
22
|
@x = x.to_f
|
21
23
|
@y = y.to_f
|
22
24
|
@data = data unless data.nil?
|
23
25
|
end
|
24
26
|
|
25
|
-
# This will calculate distance to another, given that they are
|
26
|
-
#
|
27
|
+
# This will calculate distance to another {Point}, given that they are
|
28
|
+
# both in the same 2D space.
|
27
29
|
#
|
28
|
-
# @param [Point]
|
30
|
+
# @param other [Point] the other {Point}.
|
29
31
|
# @return [Float] the distance to the other {Point}.
|
30
32
|
def distance_to(other)
|
31
33
|
Math.sqrt((other.x - self.x) ** 2 + (other.y - self.y) ** 2)
|
32
34
|
end
|
33
35
|
|
34
|
-
# This will calculate distance to another
|
35
|
-
# formula. This means that it will treat #x as longitude and #y as
|
36
|
+
# This will calculate distance to another {Point} using the Haversine
|
37
|
+
# formula. This means that it will treat {#x} as longitude and {#y} as
|
36
38
|
# latitude!
|
37
39
|
#
|
38
40
|
# a = sin²(Δφ/2) + cos φ_1 ⋅ cos φ_2 ⋅ sin²(Δλ/2)
|
41
|
+
#
|
39
42
|
# c = 2 ⋅ atan2( √a, √(1−a) )
|
43
|
+
#
|
40
44
|
# d = R ⋅ c
|
41
45
|
#
|
42
46
|
# where φ is latitude, λ is longitude, R is earth’s radius (mean
|
43
47
|
# radius = 6 371 km);
|
44
48
|
# note that angles need to be in radians to pass to trig functions!
|
45
49
|
#
|
46
|
-
# @param [Point]
|
50
|
+
# @param other [Point] the other {Point}.
|
47
51
|
# @return [Float] the distance, in meters, to the other {Point}.
|
48
52
|
def haversine_distance_to(other)
|
49
53
|
# earth's radius
|
data/lib/quadtree/quadtree.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Quadtree
|
2
|
-
# A Quadtree
|
2
|
+
# A Quadtree.
|
3
3
|
class Quadtree
|
4
4
|
|
5
5
|
# Arbitrary constant to indicate how many elements can be stored in this
|
6
6
|
# quad tree node.
|
7
|
-
# @return [Integer]
|
7
|
+
# @return [Integer] number of {Point}s this {Quadtree} can hold.
|
8
8
|
NODE_CAPACITY = 4
|
9
9
|
|
10
10
|
# Axis-aligned bounding box stored as a center with half-dimensions to
|
@@ -18,15 +18,23 @@ module Quadtree
|
|
18
18
|
|
19
19
|
# Children
|
20
20
|
|
21
|
+
# North west corner of this quad.
|
21
22
|
# @return [Quadtree]
|
22
23
|
attr_accessor :north_west
|
24
|
+
|
25
|
+
# North east corner of this quad.
|
23
26
|
# @return [Quadtree]
|
24
27
|
attr_accessor :north_east
|
28
|
+
|
29
|
+
# South west corner of this quad.
|
25
30
|
# @return [Quadtree]
|
26
31
|
attr_accessor :south_west
|
32
|
+
|
33
|
+
# South east corner of this quad.
|
27
34
|
# @return [Quadtree]
|
28
35
|
attr_accessor :south_east
|
29
36
|
|
37
|
+
# @param boundary [AxisAlignedBoundingBox] the boundary for this {Quadtree}
|
30
38
|
def initialize(boundary)
|
31
39
|
self.boundary = boundary
|
32
40
|
self.points = []
|
@@ -36,8 +44,10 @@ module Quadtree
|
|
36
44
|
self.south_east = nil
|
37
45
|
end
|
38
46
|
|
39
|
-
#
|
40
|
-
#
|
47
|
+
# Insert a {Point} in this {Quadtree}.
|
48
|
+
#
|
49
|
+
# @param point [Point] the point to insert.
|
50
|
+
# @return [Boolean] +true+ on success, +false+ otherwise.
|
41
51
|
def insert!(point)
|
42
52
|
return false unless self.boundary.contains_point?(point)
|
43
53
|
|
@@ -57,7 +67,7 @@ module Quadtree
|
|
57
67
|
|
58
68
|
# Finds all points contained within a range.
|
59
69
|
#
|
60
|
-
# @param [AxisAlignedBoundingBox]
|
70
|
+
# @param range [AxisAlignedBoundingBox] the range to search within.
|
61
71
|
# @return [Array<Point>]
|
62
72
|
def query_range(range)
|
63
73
|
# Prepare an array of results
|
data/lib/quadtree/version.rb
CHANGED
data/lib/quadtree.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quadtree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Lindblom
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|