quadtree 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc9cc583b61c883d9a87db4c67b44aba28c850afa772d7af3ffe653586380f1e
4
+ data.tar.gz: f7b082ebf586454e003e617bb7129ccf0da6527bec7dee815c6df17c0ba6da2a
5
+ SHA512:
6
+ metadata.gz: dc01d1dec2bdb2331ef0130aa86c6a84a857541c140cfc8544768ec1abd5d9d53a94e2a881dd2c2b0b12f292e280880a9763e4bf5f924f88f4dd85cea08102fb
7
+ data.tar.gz: c48b82659ef22d240587d48a48051d87dd121f0cc21439d0a2349055516888f93196d21c0409c5fb7bfc300f8c0fce5d93e9dbbbdb44f25333668ca62b84ec5a
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,208 @@
1
+
2
+ # Created by https://www.gitignore.io/api/vim,ruby,linux,emacs,macos,windows,visualstudiocode
3
+
4
+ ### Emacs ###
5
+ # -*- mode: gitignore; -*-
6
+ *~
7
+ \#*\#
8
+ /.emacs.desktop
9
+ /.emacs.desktop.lock
10
+ *.elc
11
+ auto-save-list
12
+ tramp
13
+ .\#*
14
+
15
+ # Org-mode
16
+ .org-id-locations
17
+ *_archive
18
+
19
+ # flymake-mode
20
+ *_flymake.*
21
+
22
+ # eshell files
23
+ /eshell/history
24
+ /eshell/lastdir
25
+
26
+ # elpa packages
27
+ /elpa/
28
+
29
+ # reftex files
30
+ *.rel
31
+
32
+ # AUCTeX auto folder
33
+ /auto/
34
+
35
+ # cask packages
36
+ .cask/
37
+ dist/
38
+
39
+ # Flycheck
40
+ flycheck_*.el
41
+
42
+ # server auth directory
43
+ /server/
44
+
45
+ # projectiles files
46
+ .projectile
47
+ projectile-bookmarks.eld
48
+
49
+ # directory configuration
50
+ .dir-locals.el
51
+
52
+ # saveplace
53
+ places
54
+
55
+ # url cache
56
+ url/cache/
57
+
58
+ # cedet
59
+ ede-projects.el
60
+
61
+ # smex
62
+ smex-items
63
+
64
+ # company-statistics
65
+ company-statistics-cache.el
66
+
67
+ # anaconda-mode
68
+ anaconda-mode/
69
+
70
+ ### Linux ###
71
+
72
+ # temporary files which can be created if a process still has a handle open of a deleted file
73
+ .fuse_hidden*
74
+
75
+ # KDE directory preferences
76
+ .directory
77
+
78
+ # Linux trash folder which might appear on any partition or disk
79
+ .Trash-*
80
+
81
+ # .nfs files are created when an open file is removed but is still being accessed
82
+ .nfs*
83
+
84
+ ### macOS ###
85
+ *.DS_Store
86
+ .AppleDouble
87
+ .LSOverride
88
+
89
+ # Icon must end with two \r
90
+ Icon
91
+
92
+ # Thumbnails
93
+ ._*
94
+
95
+ # Files that might appear in the root of a volume
96
+ .DocumentRevisions-V100
97
+ .fseventsd
98
+ .Spotlight-V100
99
+ .TemporaryItems
100
+ .Trashes
101
+ .VolumeIcon.icns
102
+ .com.apple.timemachine.donotpresent
103
+
104
+ # Directories potentially created on remote AFP share
105
+ .AppleDB
106
+ .AppleDesktop
107
+ Network Trash Folder
108
+ Temporary Items
109
+ .apdisk
110
+
111
+ ### Ruby ###
112
+ *.gem
113
+ *.rbc
114
+ /.config
115
+ /coverage/
116
+ /InstalledFiles
117
+ /pkg/
118
+ /spec/reports/
119
+ /spec/examples.txt
120
+ /test/tmp/
121
+ /test/version_tmp/
122
+ /tmp/
123
+
124
+ # rspec failure tracking
125
+ .rspec_status
126
+
127
+
128
+ # Used by dotenv library to load environment variables.
129
+ .env
130
+
131
+ ## Specific to RubyMotion:
132
+ .dat*
133
+ .repl_history
134
+ build/
135
+ *.bridgesupport
136
+ build-iPhoneOS/
137
+ build-iPhoneSimulator/
138
+
139
+ ## Specific to RubyMotion (use of CocoaPods):
140
+ #
141
+ # We recommend against adding the Pods directory to your .gitignore. However
142
+ # you should judge for yourself, the pros and cons are mentioned at:
143
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
144
+ #
145
+ # vendor/Pods/
146
+
147
+ ## Documentation cache and generated files:
148
+ /.yardoc/
149
+ /_yardoc/
150
+ /doc/
151
+ /rdoc/
152
+
153
+ ## Environment normalization:
154
+ /.bundle/
155
+ /vendor/bundle
156
+ /lib/bundler/man/
157
+
158
+ # for a library or gem, you might want to ignore these files since the code is
159
+ # intended to run in multiple environments; otherwise, check them in:
160
+ Gemfile.lock
161
+ # .ruby-version
162
+ # .ruby-gemset
163
+
164
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
165
+ .rvmrc
166
+
167
+ ### Vim ###
168
+ # swap
169
+ .sw[a-p]
170
+ .*.sw[a-p]
171
+ # session
172
+ Session.vim
173
+ # temporary
174
+ .netrwhist
175
+ # auto-generated tag files
176
+ tags
177
+
178
+ ### VisualStudioCode ###
179
+ .vscode/*
180
+ !.vscode/settings.json
181
+ !.vscode/tasks.json
182
+ !.vscode/launch.json
183
+ !.vscode/extensions.json
184
+ .history
185
+
186
+ ### Windows ###
187
+ # Windows thumbnail cache files
188
+ Thumbs.db
189
+ ehthumbs.db
190
+ ehthumbs_vista.db
191
+
192
+ # Folder config file
193
+ Desktop.ini
194
+
195
+ # Recycle Bin used on file shares
196
+ $RECYCLE.BIN/
197
+
198
+ # Windows Installer files
199
+ *.cab
200
+ *.msi
201
+ *.msm
202
+ *.msp
203
+
204
+ # Windows shortcuts
205
+ *.lnk
206
+
207
+
208
+ # End of https://www.gitignore.io/api/vim,ruby,linux,emacs,macos,windows,visualstudiocode
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jan.lindblom@mittmedia.se. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in quadtree.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jan Lindblom
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Quadtree
2
+
3
+ Quadtrees in Ruby. For searching spatially related nodes in some space, you know.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'quadtree'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install quadtree
20
+
21
+ ## Usage
22
+
23
+ Load it in your code to start building quadtrees:
24
+
25
+ ```ruby
26
+ require 'quadtree'
27
+
28
+ boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
29
+ qt = Quadtree::Quadtree.new(boundary)
30
+ ```
31
+
32
+ Then you can do lookups and such:
33
+
34
+ ```ruby
35
+ getaboden = Quadtree::Point.new(19.8470050, 60.3747940, "Getaboden")
36
+ knutnas = Quadtree::Point.new(19.8271170, 60.3505570, "Knutnäs")
37
+ boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
38
+ boundary2 = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 4472.0)
39
+ qt.insert! getaboden
40
+ qt.insert! knutnas
41
+ qt.query_range(boundary2)
42
+ # [#<Quadtree::Point:0x00007fdcb19e0698 @data="Getaboden", @x=19.847005, @y=60.374794>,
43
+ #<Quadtree::Point:0x00007fdcb19ec7b8 @data="Knutnäs", @x=19.827117, @y=60.350557>]
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/janlindblom/ruby-quadtree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
59
+
60
+ ## Code of Conduct
61
+
62
+ Everyone interacting in the Quadtree project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/quadtree/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "yard"
4
+ require "yard/rake/yardoc_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ YARD::Rake::YardocTask.new do |t|
9
+ t.files = ['lib/**/*.rb']
10
+ t.stats_options = ['--list-undoc']
11
+ end
12
+
13
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "quadtree"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
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
+ image: ruby:2.4.0
4
+
5
+ pipelines:
6
+ default:
7
+ - step:
8
+ caches:
9
+ - bundler
10
+ script:
11
+ - bundle install
12
+ - bundle exec rake spec
13
+
14
+ definitions:
15
+ caches:
16
+ bundler: ./vendor
data/lib/quadtree.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "quadtree/version"
2
+
3
+ require "quadtree/point"
4
+ require "quadtree/axis_aligned_bounding_box"
5
+ require "quadtree/quadtree"
6
+
7
+ # Quadtrees.
8
+ #
9
+ # @since 1.0.0
10
+ # @author Jan Lindblom <janlindblom@fastmail.fm>
11
+ module Quadtree
12
+
13
+ end
@@ -0,0 +1,82 @@
1
+ module Quadtree
2
+ # Axis-aligned bounding box with half dimension and center.
3
+ class AxisAlignedBoundingBox
4
+
5
+ # @return [Point]
6
+ attr_accessor :center
7
+ # @return [Float]
8
+ attr_accessor :half_dimension
9
+
10
+ # @param [Point] center
11
+ # @param [Float] half_dimension
12
+ def initialize(center, half_dimension)
13
+ @center = center
14
+ @half_dimension = half_dimension.to_f
15
+ end
16
+
17
+ # @param [Point] point
18
+ # @return [Boolean]
19
+ def contains_point?(point)
20
+ if point.x >= self.center.x - self.half_dimension and point.x <= self.center.x + self.half_dimension
21
+ if point.y >= self.center.y - self.half_dimension and point.y <= self.center.y + self.half_dimension
22
+ return true
23
+ end
24
+ end
25
+ false
26
+ end
27
+
28
+ # @param [AxisAlignedBoundingBox] other
29
+ # @return [Boolean]
30
+ def intersects?(other)
31
+ other_lt_corner = Point.new(other.left, other.top)
32
+ other_rt_corner = Point.new(other.right, other.top)
33
+ other_lb_corner = Point.new(other.left, other.bottom)
34
+ other_rb_corner = Point.new(other.right, other.bottom)
35
+
36
+ [other_lt_corner, other_rt_corner, other_lb_corner, other_rb_corner].each do |corner|
37
+ return true if self.contains_point?(corner)
38
+ end
39
+ false
40
+ end
41
+
42
+ # @return [Float]
43
+ def left
44
+ @center.x - @half_dimension
45
+ end
46
+
47
+ # @return [Float]
48
+ def right
49
+ @center.x + @half_dimension
50
+ end
51
+
52
+ # Get the Y coordinate of the top edge of this AABB.
53
+ #
54
+ # @return [Float] the Y coordinate of the top edge of this AABB.
55
+ def top
56
+ @center.y + @half_dimension
57
+ end
58
+
59
+ # Get the Y coordinate of the bottom edge of this AABB.
60
+ #
61
+ # @return [Float] the Y coordinate of the bottom edge of this AABB.
62
+ def bottom
63
+ @center.y - @half_dimension
64
+ end
65
+
66
+ # @return [Float]
67
+ def width
68
+ span
69
+ end
70
+
71
+ # @return [Float]
72
+ def height
73
+ span
74
+ end
75
+
76
+ private
77
+
78
+ def span
79
+ @half_dimension * 2
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,66 @@
1
+ module Quadtree
2
+ # Simple coordinate object to represent points in some space.
3
+ class Point
4
+
5
+ # @return [Float] X coordinate
6
+ attr_accessor :x
7
+
8
+ # @return [Float] Y coordinate
9
+ attr_accessor :y
10
+
11
+ # Payload attached to this {Point}.
12
+ attr_accessor :data
13
+
14
+ # Create a new {Point}.
15
+ #
16
+ # @param [Float] x X coordinate
17
+ # @param [Float] y Y coordinate
18
+ def initialize(x, y, data=nil)
19
+ @x = x
20
+ @y = y
21
+ @data = data unless data.nil?
22
+ end
23
+
24
+ # This will calculate distance to another, given that they are both
25
+ # on the same flat two dimensional plane.
26
+ #
27
+ # @param [Point] other the other {Point}.
28
+ # @return [Float] the distance to the other {Point}.
29
+ def distance_to(other)
30
+ Math.sqrt((other.x - self.x) ** 2 + (other.y - self.y) ** 2)
31
+ end
32
+
33
+ # This will calculate distance to another point using the Haversine
34
+ # formula. This means that it will treat #x as longitude and #y as
35
+ # latitude!
36
+ #
37
+ # a = sin²(Δφ/2) + cos φ_1 ⋅ cos φ_2 ⋅ sin²(Δλ/2)
38
+ # c = 2 ⋅ atan2( √a, √(1−a) )
39
+ # d = R ⋅ c
40
+ #
41
+ # where φ is latitude, λ is longitude, R is earth’s radius (mean
42
+ # radius = 6 371 km);
43
+ # note that angles need to be in radians to pass to trig functions!
44
+ #
45
+ # @param [Point] other the other {Point}.
46
+ # @return [Float] the distance, in meters, to the other {Point}.
47
+ def haversine_distance_to(other)
48
+ # earth's radius
49
+ r = 6371 * 1000.0
50
+ # 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)
55
+
56
+ # a = sin²(Δφ/2) + cos φ_1 ⋅ cos φ_2 ⋅ sin²(Δλ/2)
57
+ a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
58
+ Math.cos(lat1) * Math.cos(lat2) *
59
+ Math.sin(dlon / 2) * Math.sin(dlon / 2)
60
+ # c = 2 ⋅ atan2( √a, √(1−a) )
61
+ c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
62
+ # d = R ⋅ c
63
+ return r * c
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,116 @@
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,0 +1,3 @@
1
+ module Quadtree
2
+ VERSION = "1.0.1"
3
+ end
data/quadtree.gemspec ADDED
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "quadtree/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quadtree"
8
+ spec.version = Quadtree::VERSION
9
+ spec.authors = ["Jan Lindblom"]
10
+ spec.email = ["jan.lindblom@mittmedia.se"]
11
+
12
+ spec.summary = %q{Quadtrees in Ruby.}
13
+ spec.homepage = "https://bitbucket.org/janlindblom/ruby-quadtree"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "pry", "~> 0.11"
27
+ spec.add_development_dependency "yard", "~> 0.9"
28
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quadtree
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan Lindblom
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ description:
84
+ email:
85
+ - jan.lindblom@mittmedia.se
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".editorconfig"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - bitbucket-pipelines.yml
102
+ - lib/quadtree.rb
103
+ - lib/quadtree/axis_aligned_bounding_box.rb
104
+ - lib/quadtree/point.rb
105
+ - lib/quadtree/quadtree.rb
106
+ - lib/quadtree/version.rb
107
+ - quadtree.gemspec
108
+ homepage: https://bitbucket.org/janlindblom/ruby-quadtree
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.7.3
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Quadtrees in Ruby.
132
+ test_files: []