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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 93b38a02013f5c97126e742d2262be25f388e4c1
4
- data.tar.gz: 82bb915f6bddf1f6c1f187cbaf5a3a6eb3a47b0d
2
+ SHA256:
3
+ metadata.gz: 6cc1a164a8db13d4faf288086e402c3f640e7df3565c06fca0bcc412349e1c82
4
+ data.tar.gz: 7775b6a0a06fda4e720e8506e0bc6d5366a47097547df4ef92425d81e5d57756
5
5
  SHA512:
6
- metadata.gz: f18c67ee620d739ddda87a0550f06eb770bb512a7781a0f1d534bdb3cbc51ee1d39fab47e96af94b2d7e75ad298d3b85bdc98ddb48d102efc53fe62be6233397
7
- data.tar.gz: 3265235a06f9b067928906086849cca271a78eba0eec2efa48b6e4b6a1e065f006fa8d9274501724101c84e1ba0b69c7687eeac317312bd367f12bf4e7772381
6
+ metadata.gz: ddc066fdc14f81a13648370cf5f11391287c5ecca6914982c90b85491acfc74399acfbe52bff2c88a621a989624c4d2177d6743b5deb6e492ede6e9093f620c6
7
+ data.tar.gz: 87ccb5227454748c3c9a7eb9611a332976d9677023b8106b9de132e1a768b5a9cdcbdadd806cb4317585ad28e859fb7d8cb099ddb39f9e242ac9583abdaa65ba
@@ -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
@@ -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://github.com/[USERNAME]/quadtree/blob/master/CODE_OF_CONDUCT.md).
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 == "bitbucket-pipelines.yml" ||
28
- f == "buildspec.yml"
29
- end
30
- s.bindir = "exe"
31
- s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
- s.require_paths = ["lib"]
33
-
34
- s.required_ruby_version = '>= 2.3.0'
35
-
36
- s.add_development_dependency "bundler", "~> 1.14"
37
- s.add_development_dependency "rake", "~> 10.0"
38
- s.add_development_dependency "rspec", "~> 3.0"
39
- s.add_development_dependency "pry", "~> 0.11"
40
- s.add_development_dependency "yard", "~> 0.9"
41
- s.add_development_dependency "simplecov", "~> 0.16"
42
- s.add_development_dependency "rubocop", "~> 0.54"
43
- s.add_runtime_dependency "version", "~> 1.1"
44
- end
45
-
46
- Rake::VersionTask.new do |task|
47
- task.with_gemspec = spec
48
- task.with_git = false
49
- end
50
-
51
- YARD::Rake::YardocTask.new do |t|
52
- t.files = ['lib/**/*.rb']
53
- t.stats_options = ['--list-undoc']
54
- end
55
-
56
- RuboCop::RakeTask.new(:rubocop) do |task|
57
- task.patterns = ['lib/**/*.rb']
58
- # only show the files with failures
59
- task.formatters = ['worst']
60
- # don't abort rake on failure
61
- task.fail_on_error = false
62
- end
63
-
64
- task :default => :spec
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.7
1
+ 1.0.8
@@ -1,14 +1,16 @@
1
- require "version"
2
- require "quadtree/version"
3
-
4
- require "quadtree/unknown_type_error"
5
-
6
- require "quadtree/point"
7
- require "quadtree/axis_aligned_bounding_box"
8
- require "quadtree/quadtree"
9
-
10
- # Quadtrees.
11
- #
12
- # @author Jan Lindblom <janlindblom@fastmail.fm>
13
- module Quadtree
14
- end
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
- module Quadtree
2
- # Axis-aligned bounding box with half dimension and center.
3
- class AxisAlignedBoundingBox
4
-
5
- # Center {Point} of this instance.
6
- #
7
- # @return [Point] the {Point} marking the center of this instance.
8
- attr_accessor :center
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.
14
- attr_accessor :half_dimension
15
-
16
- # @param center [Point]
17
- # @param half_dimension [Float]
18
- def initialize(center, half_dimension)
19
- @center = center
20
- @half_dimension = half_dimension.to_f
21
- end
22
-
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.
28
- def contains_point?(point)
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
33
- return true
34
- end
35
- end
36
- false
37
- end
38
-
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.
43
- def intersects?(other)
44
- other_lt_corner = Point.new(other.left, other.top)
45
- other_rt_corner = Point.new(other.right, other.top)
46
- other_lb_corner = Point.new(other.left, other.bottom)
47
- other_rb_corner = Point.new(other.right, other.bottom)
48
-
49
- [
50
- other_lt_corner,
51
- other_rt_corner,
52
- other_lb_corner,
53
- other_rb_corner
54
- ].each do |corner|
55
- return true if self.contains_point?(corner)
56
- end
57
- false
58
- end
59
-
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.
63
- def left
64
- @center.x - @half_dimension
65
- end
66
-
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.
70
- def right
71
- @center.x + @half_dimension
72
- end
73
-
74
- # Get the Y coordinate of the top edge of this instance.
75
- #
76
- # @return [Float] the Y coordinate of the top edge of this instance.
77
- def top
78
- @center.y + @half_dimension
79
- end
80
-
81
- # Get the Y coordinate of the bottom edge of this instance.
82
- #
83
- # @return [Float] the Y coordinate of the bottom edge of this instance.
84
- def bottom
85
- @center.y - @half_dimension
86
- end
87
-
88
- # Get the width of this instance.
89
- #
90
- # @return [Float]
91
- def width
92
- span
93
- end
94
-
95
- # Get the height of this instance.
96
- #
97
- # @return [Float]
98
- def height
99
- span
100
- end
101
-
102
- private
103
-
104
- def span
105
- @half_dimension * 2
106
- end
107
- end
108
- end
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