quadtree 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +28 -0
- data/.rubocop_todo.yml +76 -0
- data/README.md +67 -67
- data/Rakefile +69 -64
- data/VERSION +1 -1
- data/lib/quadtree.rb +16 -14
- data/lib/quadtree/axis_aligned_bounding_box.rb +155 -108
- data/lib/quadtree/point.rb +161 -107
- data/lib/quadtree/quadtree.rb +222 -148
- data/lib/quadtree/unknown_type_error.rb +8 -7
- data/lib/quadtree/version.rb +7 -5
- data/quadtree.gemspec +56 -53
- metadata +31 -18
- data/.editorconfig +0 -12
- data/.gitignore +0 -208
- data/.rspec +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6cc1a164a8db13d4faf288086e402c3f640e7df3565c06fca0bcc412349e1c82
|
4
|
+
data.tar.gz: 7775b6a0a06fda4e720e8506e0bc6d5366a47097547df4ef92425d81e5d57756
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddc066fdc14f81a13648370cf5f11391287c5ecca6914982c90b85491acfc74399acfbe52bff2c88a621a989624c4d2177d6743b5deb6e492ede6e9093f620c6
|
7
|
+
data.tar.gz: 87ccb5227454748c3c9a7eb9611a332976d9677023b8106b9de132e1a768b5a9cdcbdadd806cb4317585ad28e859fb7d8cb099ddb39f9e242ac9583abdaa65ba
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
4
|
+
Enabled: true
|
5
|
+
Layout/SpaceAroundMethodCallOperator:
|
6
|
+
Enabled: true
|
7
|
+
Lint/DeprecatedOpenSSLConstant:
|
8
|
+
Enabled: true
|
9
|
+
Lint/MixedRegexpCaptureTypes:
|
10
|
+
Enabled: true
|
11
|
+
Lint/RaiseException:
|
12
|
+
Enabled: true
|
13
|
+
Lint/StructNewOverride:
|
14
|
+
Enabled: true
|
15
|
+
Style/ExponentialNotation:
|
16
|
+
Enabled: true
|
17
|
+
Style/HashEachMethods:
|
18
|
+
Enabled: true
|
19
|
+
Style/HashTransformKeys:
|
20
|
+
Enabled: true
|
21
|
+
Style/HashTransformValues:
|
22
|
+
Enabled: true
|
23
|
+
Style/RedundantRegexpCharacterClass:
|
24
|
+
Enabled: true
|
25
|
+
Style/RedundantRegexpEscape:
|
26
|
+
Enabled: true
|
27
|
+
Style/SlicingWithRange:
|
28
|
+
Enabled: true
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2020-06-04 20:16:28 +0300 using RuboCop version 0.85.0.
|
4
|
+
|
5
|
+
# Configuration parameters: EnforcedStyle.
|
6
|
+
# SupportedStyles: native, lf, crlf
|
7
|
+
Layout/EndOfLine:
|
8
|
+
EnforcedStyle: native
|
9
|
+
|
10
|
+
# Configuration parameters: IgnoredMethods.
|
11
|
+
Metrics/AbcSize:
|
12
|
+
Enabled: true
|
13
|
+
Max: 25
|
14
|
+
Exclude:
|
15
|
+
- lib/quadtree/quadtree.rb
|
16
|
+
|
17
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
18
|
+
# ExcludedMethods: refine
|
19
|
+
Metrics/BlockLength:
|
20
|
+
CountComments: false
|
21
|
+
#Max: 47
|
22
|
+
|
23
|
+
# Configuration parameters: CountComments.
|
24
|
+
Metrics/ClassLength:
|
25
|
+
CountComments: false
|
26
|
+
Exclude:
|
27
|
+
- lib/quadtree/quadtree.rb
|
28
|
+
|
29
|
+
# Configuration parameters: IgnoredMethods.
|
30
|
+
Metrics/CyclomaticComplexity:
|
31
|
+
Enabled: true
|
32
|
+
Exclude:
|
33
|
+
- lib/quadtree/quadtree.rb
|
34
|
+
|
35
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
36
|
+
Metrics/MethodLength:
|
37
|
+
CountComments: false
|
38
|
+
Exclude:
|
39
|
+
- lib/quadtree/quadtree.rb
|
40
|
+
|
41
|
+
# Configuration parameters: CountKeywordArgs.
|
42
|
+
Metrics/ParameterLists:
|
43
|
+
Enabled: true
|
44
|
+
Exclude:
|
45
|
+
- lib/quadtree/quadtree.rb
|
46
|
+
|
47
|
+
# Configuration parameters: IgnoredMethods.
|
48
|
+
Metrics/PerceivedComplexity:
|
49
|
+
Enabled: true
|
50
|
+
Exclude:
|
51
|
+
- lib/quadtree/quadtree.rb
|
52
|
+
|
53
|
+
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
|
54
|
+
# AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp
|
55
|
+
Naming/MethodParameterName:
|
56
|
+
Enabled: true
|
57
|
+
Exclude:
|
58
|
+
- lib/quadtree/point.rb
|
59
|
+
|
60
|
+
# Configuration parameters: AllowedChars.
|
61
|
+
Style/AsciiComments:
|
62
|
+
Enabled: true
|
63
|
+
Exclude:
|
64
|
+
- lib/quadtree/point.rb
|
65
|
+
|
66
|
+
Style/Documentation:
|
67
|
+
Enabled: true
|
68
|
+
|
69
|
+
# Configuration parameters: Strict.
|
70
|
+
Style/NumericLiterals:
|
71
|
+
Enabled: true
|
72
|
+
|
73
|
+
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
74
|
+
# URISchemes: http, https
|
75
|
+
Layout/LineLength:
|
76
|
+
Enabled: true
|
data/README.md
CHANGED
@@ -1,67 +1,67 @@
|
|
1
|
-
# Quadtree
|
2
|
-
|
3
|
-
![Build Status](https://codebuild.us-east-1.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoidk1aMm1Ya212SjRsU1pVdWNVZ2FRTDVPUXlVM2NsY0dWU2UxL1dEUlh2VTdxdURhcmxRK1JDTGg5OXJxeURKeFRiZXV5VEtTbmRBZCt6bTdDTms1Rm5ZPSIsIml2UGFyYW1ldGVyU3BlYyI6ImlUamttMGtUckZRT0tvM0EiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master)
|
4
|
-
[![Bitbucket Pipelines](https://img.shields.io/bitbucket/pipelines/janlindblom/ruby-quadtree.png)](https://bitbucket.org/janlindblom/ruby-quadtree)
|
5
|
-
[![Gem](https://img.shields.io/gem/v/quadtree.png)](https://rubygems.org/gems/quadtree)
|
6
|
-
[![Documentation](https://img.shields.io/badge/docs-rdoc.info-yellow.png)](http://www.rubydoc.info/gems/quadtree/frames)
|
7
|
-
|
8
|
-
Quadtrees in Ruby. For searching spatially related nodes in some space, you know.
|
9
|
-
|
10
|
-
## Installation
|
11
|
-
|
12
|
-
Add this line to your application's Gemfile:
|
13
|
-
|
14
|
-
```ruby
|
15
|
-
gem 'quadtree'
|
16
|
-
```
|
17
|
-
|
18
|
-
And then execute:
|
19
|
-
|
20
|
-
$ bundle
|
21
|
-
|
22
|
-
Or install it yourself as:
|
23
|
-
|
24
|
-
$ gem install quadtree
|
25
|
-
|
26
|
-
## Usage
|
27
|
-
|
28
|
-
Load it in your code to start building quadtrees:
|
29
|
-
|
30
|
-
```ruby
|
31
|
-
require 'quadtree'
|
32
|
-
|
33
|
-
boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
|
34
|
-
qt = Quadtree::Quadtree.new(boundary)
|
35
|
-
```
|
36
|
-
|
37
|
-
Then you can do lookups and such:
|
38
|
-
|
39
|
-
```ruby
|
40
|
-
getaboden = Quadtree::Point.new(19.8470050, 60.3747940, "Getaboden")
|
41
|
-
knutnas = Quadtree::Point.new(19.8271170, 60.3505570, "Knutnäs")
|
42
|
-
boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
|
43
|
-
boundary2 = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 4472.0)
|
44
|
-
qt.insert! getaboden
|
45
|
-
qt.insert! knutnas
|
46
|
-
qt.query_range(boundary2)
|
47
|
-
# [#<Quadtree::Point:0x00007fdcb19e0698 @data="Getaboden", @x=19.847005, @y=60.374794>,
|
48
|
-
#<Quadtree::Point:0x00007fdcb19ec7b8 @data="Knutnäs", @x=19.827117, @y=60.350557>]
|
49
|
-
```
|
50
|
-
|
51
|
-
## Development
|
52
|
-
|
53
|
-
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.
|
54
|
-
|
55
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number using `rake version:create` or `rake version:bump`, 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).
|
56
|
-
|
57
|
-
## Contributing
|
58
|
-
|
59
|
-
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.
|
60
|
-
|
61
|
-
## License
|
62
|
-
|
63
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
64
|
-
|
65
|
-
## Code of Conduct
|
66
|
-
|
67
|
-
Everyone interacting in the Quadtree project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://
|
1
|
+
# Quadtree
|
2
|
+
|
3
|
+
![Build Status](https://codebuild.us-east-1.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoidk1aMm1Ya212SjRsU1pVdWNVZ2FRTDVPUXlVM2NsY0dWU2UxL1dEUlh2VTdxdURhcmxRK1JDTGg5OXJxeURKeFRiZXV5VEtTbmRBZCt6bTdDTms1Rm5ZPSIsIml2UGFyYW1ldGVyU3BlYyI6ImlUamttMGtUckZRT0tvM0EiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master)
|
4
|
+
[![Bitbucket Pipelines](https://img.shields.io/bitbucket/pipelines/janlindblom/ruby-quadtree.png)](https://bitbucket.org/janlindblom/ruby-quadtree)
|
5
|
+
[![Gem](https://img.shields.io/gem/v/quadtree.png)](https://rubygems.org/gems/quadtree)
|
6
|
+
[![Documentation](https://img.shields.io/badge/docs-rdoc.info-yellow.png)](http://www.rubydoc.info/gems/quadtree/frames)
|
7
|
+
|
8
|
+
Quadtrees in Ruby. For searching spatially related nodes in some space, you know.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'quadtree'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install quadtree
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Load it in your code to start building quadtrees:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'quadtree'
|
32
|
+
|
33
|
+
boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
|
34
|
+
qt = Quadtree::Quadtree.new(boundary)
|
35
|
+
```
|
36
|
+
|
37
|
+
Then you can do lookups and such:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
getaboden = Quadtree::Point.new(19.8470050, 60.3747940, "Getaboden")
|
41
|
+
knutnas = Quadtree::Point.new(19.8271170, 60.3505570, "Knutnäs")
|
42
|
+
boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
|
43
|
+
boundary2 = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 4472.0)
|
44
|
+
qt.insert! getaboden
|
45
|
+
qt.insert! knutnas
|
46
|
+
qt.query_range(boundary2)
|
47
|
+
# [#<Quadtree::Point:0x00007fdcb19e0698 @data="Getaboden", @x=19.847005, @y=60.374794>,
|
48
|
+
#<Quadtree::Point:0x00007fdcb19ec7b8 @data="Knutnäs", @x=19.827117, @y=60.350557>]
|
49
|
+
```
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
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.
|
54
|
+
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number using `rake version:create` or `rake version:bump`, 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).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
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.
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
64
|
+
|
65
|
+
## Code of Conduct
|
66
|
+
|
67
|
+
Everyone interacting in the Quadtree project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://bitbucket.org/janlindblom/ruby-quadtree/src/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -1,64 +1,69 @@
|
|
1
|
-
lib = File.expand_path("../lib", __FILE__)
|
2
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
|
4
|
-
require "rake/version_task"
|
5
|
-
require "bundler/gem_tasks"
|
6
|
-
require "rspec/core/rake_task"
|
7
|
-
require "yard"
|
8
|
-
require "yard/rake/yardoc_task"
|
9
|
-
require 'rubocop/rake_task'
|
10
|
-
require "quadtree"
|
11
|
-
|
12
|
-
RSpec::Core::RakeTask.new(:spec)
|
13
|
-
|
14
|
-
spec = Gem::Specification.new do |s|
|
15
|
-
s.name = "quadtree"
|
16
|
-
s.version = Quadtree::VERSION
|
17
|
-
s.authors = ["Jan Lindblom"]
|
18
|
-
s.email = ["janlindblom@fastmail.fm"]
|
19
|
-
|
20
|
-
s.summary = %q{Quadtrees in Ruby.}
|
21
|
-
s.homepage = "https://bitbucket.org/janlindblom/ruby-quadtree"
|
22
|
-
s.license = "MIT"
|
23
|
-
|
24
|
-
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
-
f.match(%r{^(bin|test|spec|features)/}) ||
|
26
|
-
f == ".travis.yml" ||
|
27
|
-
f == "
|
28
|
-
f == "
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
s.
|
35
|
-
|
36
|
-
s.
|
37
|
-
|
38
|
-
s.
|
39
|
-
|
40
|
-
s.add_development_dependency "
|
41
|
-
s.add_development_dependency "
|
42
|
-
s.add_development_dependency "
|
43
|
-
s.
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
task
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require "rake/version_task"
|
5
|
+
require "bundler/gem_tasks"
|
6
|
+
require "rspec/core/rake_task"
|
7
|
+
require "yard"
|
8
|
+
require "yard/rake/yardoc_task"
|
9
|
+
require 'rubocop/rake_task'
|
10
|
+
require "quadtree"
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:spec)
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = "quadtree"
|
16
|
+
s.version = Quadtree::VERSION
|
17
|
+
s.authors = ["Jan Lindblom"]
|
18
|
+
s.email = ["janlindblom@fastmail.fm"]
|
19
|
+
|
20
|
+
s.summary = %q{Quadtrees in Ruby.}
|
21
|
+
s.homepage = "https://bitbucket.org/janlindblom/ruby-quadtree"
|
22
|
+
s.license = "MIT"
|
23
|
+
|
24
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(bin|test|spec|features)/}) ||
|
26
|
+
f == ".travis.yml" ||
|
27
|
+
f == ".gitignore" ||
|
28
|
+
f == ".editorconfig" ||
|
29
|
+
f == ".rspec" ||
|
30
|
+
f == "bitbucket-pipelines.yml" ||
|
31
|
+
f == "buildspec.yml" ||
|
32
|
+
f == ".gitlab-ci.yml"
|
33
|
+
end
|
34
|
+
s.bindir = "exe"
|
35
|
+
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
|
38
|
+
s.required_ruby_version = '>= 2.3.0'
|
39
|
+
|
40
|
+
s.add_development_dependency "bundler", "~> 2"
|
41
|
+
s.add_development_dependency "rake", "~> 13.0"
|
42
|
+
s.add_development_dependency "rspec", "~> 3.9"
|
43
|
+
s.add_development_dependency "pry", "~> 0.13"
|
44
|
+
s.add_development_dependency "yard", "~> 0.9"
|
45
|
+
s.add_development_dependency "simplecov", "~> 0.18"
|
46
|
+
s.add_development_dependency "rspec_junit_formatter", "~> 0.4"
|
47
|
+
s.add_development_dependency "rubocop", "~> 0.85"
|
48
|
+
s.add_runtime_dependency "version", "~> 1.1"
|
49
|
+
end
|
50
|
+
|
51
|
+
Rake::VersionTask.new do |task|
|
52
|
+
task.with_gemspec = spec
|
53
|
+
task.with_git = false
|
54
|
+
end
|
55
|
+
|
56
|
+
YARD::Rake::YardocTask.new do |t|
|
57
|
+
t.files = ['lib/**/*.rb']
|
58
|
+
t.stats_options = ['--list-undoc']
|
59
|
+
end
|
60
|
+
|
61
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
62
|
+
task.patterns = ['lib/**/*.rb']
|
63
|
+
# only show the files with failures
|
64
|
+
task.formatters = ['worst']
|
65
|
+
# don't abort rake on failure
|
66
|
+
task.fail_on_error = false
|
67
|
+
end
|
68
|
+
|
69
|
+
task :default => :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.8
|
data/lib/quadtree.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require
|
5
|
-
|
6
|
-
require
|
7
|
-
|
8
|
-
require
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
|
14
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'version'
|
4
|
+
require 'quadtree/version'
|
5
|
+
|
6
|
+
require 'quadtree/unknown_type_error'
|
7
|
+
|
8
|
+
require 'quadtree/point'
|
9
|
+
require 'quadtree/axis_aligned_bounding_box'
|
10
|
+
require 'quadtree/quadtree'
|
11
|
+
|
12
|
+
# Quadtrees.
|
13
|
+
#
|
14
|
+
# @author Jan Lindblom <janlindblom@fastmail.fm>
|
15
|
+
module Quadtree
|
16
|
+
end
|
@@ -1,108 +1,155 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
# @param
|
18
|
-
|
19
|
-
|
20
|
-
@
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
#
|
40
|
-
#
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
#
|
68
|
-
#
|
69
|
-
# @return [
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
#
|
76
|
-
#
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Quadtree
|
4
|
+
# Axis-aligned bounding box with half dimension and center.
|
5
|
+
class AxisAlignedBoundingBox
|
6
|
+
# Center {Point} of this instance.
|
7
|
+
#
|
8
|
+
# @return [Point] the {Point} marking the center of this instance.
|
9
|
+
attr_accessor :center
|
10
|
+
|
11
|
+
# Half dimension of this instance (distance from the center {Point} to the
|
12
|
+
# edge).
|
13
|
+
#
|
14
|
+
# @return [Float] distance from the center {Point} to the edge.
|
15
|
+
attr_accessor :half_dimension
|
16
|
+
|
17
|
+
# @param center [Point]
|
18
|
+
# @param half_dimension [Float]
|
19
|
+
def initialize(center, half_dimension)
|
20
|
+
@center = center
|
21
|
+
@half_dimension = half_dimension.to_f
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Create a Hash for this {AxisAlignedBoundingBox}.
|
26
|
+
#
|
27
|
+
# @return [Hash] Hash representation of this {AxisAlignedBoundingBox}.
|
28
|
+
#
|
29
|
+
def to_h
|
30
|
+
{
|
31
|
+
'center': center.to_h,
|
32
|
+
'half_dimension': half_dimension
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Create a Hash for this {AxisAlignedBoundingBox}.
|
38
|
+
#
|
39
|
+
# @return [Hash] Hash representation of this {AxisAlignedBoundingBox}.
|
40
|
+
#
|
41
|
+
def to_hash
|
42
|
+
to_h
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Create a JSON String representation of this {AxisAlignedBoundingBox}.
|
47
|
+
#
|
48
|
+
# @return [String] JSON String of this {AxisAlignedBoundingBox}.
|
49
|
+
#
|
50
|
+
def to_json(*_args)
|
51
|
+
require 'json'
|
52
|
+
to_h.to_json
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Create a String for this {AxisAlignedBoundingBox}.
|
57
|
+
#
|
58
|
+
# @return [String] String representation of this {AxisAlignedBoundingBox}.
|
59
|
+
#
|
60
|
+
def to_s
|
61
|
+
to_h.to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Construct a {AxisAlignedBoundingBox} from a JSON String.
|
66
|
+
#
|
67
|
+
# @param [String] json_data input JSON String.
|
68
|
+
#
|
69
|
+
# @return [AxisAlignedBoundingBox] the {AxisAlignedBoundingBox} contained in the JSON String.
|
70
|
+
#
|
71
|
+
def self.from_json(json_data)
|
72
|
+
new(Point.from_json(json_data['center']), json_data['half_dimension'])
|
73
|
+
end
|
74
|
+
|
75
|
+
# Check if this instance contains a given {Point}.
|
76
|
+
#
|
77
|
+
# @param point [Point] the {Point} to check for.
|
78
|
+
# @return [Boolean] +true+ if given {Point} is contained, +false+
|
79
|
+
# otherwise.
|
80
|
+
def contains_point?(point)
|
81
|
+
if point.x >= center.x - half_dimension &&
|
82
|
+
point.x <= center.x + half_dimension
|
83
|
+
if point.y >= center.y - half_dimension &&
|
84
|
+
point.y <= center.y + half_dimension
|
85
|
+
return true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
false
|
89
|
+
end
|
90
|
+
|
91
|
+
# Check if this instance intersects with another instance.
|
92
|
+
#
|
93
|
+
# @param other [AxisAlignedBoundingBox] the other instance to check for.
|
94
|
+
# @return [Boolean] +true+ if these intersects, +false+ otherwise.
|
95
|
+
def intersects?(other)
|
96
|
+
other_lt_corner = Point.new(other.left, other.top)
|
97
|
+
other_rt_corner = Point.new(other.right, other.top)
|
98
|
+
other_lb_corner = Point.new(other.left, other.bottom)
|
99
|
+
other_rb_corner = Point.new(other.right, other.bottom)
|
100
|
+
|
101
|
+
[other_lt_corner, other_rt_corner, other_lb_corner, other_rb_corner].each do |corner|
|
102
|
+
return true if contains_point?(corner)
|
103
|
+
end
|
104
|
+
false
|
105
|
+
end
|
106
|
+
|
107
|
+
# Get the X coordinate of the left edge of this instance.
|
108
|
+
#
|
109
|
+
# @return [Float] the X coordinate of the left edge of this instance.
|
110
|
+
def left
|
111
|
+
@center.x - @half_dimension
|
112
|
+
end
|
113
|
+
|
114
|
+
# Get the X coordinate of the right edge of this instance.
|
115
|
+
#
|
116
|
+
# @return [Float] the X coordinate of the right edge of this instance.
|
117
|
+
def right
|
118
|
+
@center.x + @half_dimension
|
119
|
+
end
|
120
|
+
|
121
|
+
# Get the Y coordinate of the top edge of this instance.
|
122
|
+
#
|
123
|
+
# @return [Float] the Y coordinate of the top edge of this instance.
|
124
|
+
def top
|
125
|
+
@center.y + @half_dimension
|
126
|
+
end
|
127
|
+
|
128
|
+
# Get the Y coordinate of the bottom edge of this instance.
|
129
|
+
#
|
130
|
+
# @return [Float] the Y coordinate of the bottom edge of this instance.
|
131
|
+
def bottom
|
132
|
+
@center.y - @half_dimension
|
133
|
+
end
|
134
|
+
|
135
|
+
# Get the width of this instance.
|
136
|
+
#
|
137
|
+
# @return [Float]
|
138
|
+
def width
|
139
|
+
span
|
140
|
+
end
|
141
|
+
|
142
|
+
# Get the height of this instance.
|
143
|
+
#
|
144
|
+
# @return [Float]
|
145
|
+
def height
|
146
|
+
span
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def span
|
152
|
+
@half_dimension * 2
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|