quadtree 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CODE_OF_CONDUCT.md CHANGED
@@ -1,74 +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/
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 CHANGED
@@ -1,6 +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
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 CHANGED
@@ -1,21 +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.
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 CHANGED
@@ -1,64 +1,65 @@
1
- # Quadtree
2
-
3
- [![Gem Version](https://badge.fury.io/rb/quadtree.svg)](https://badge.fury.io/rb/quadtree)
4
-
5
- Quadtrees in Ruby. For searching spatially related nodes in some space, you know.
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'quadtree'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install quadtree
22
-
23
- ## Usage
24
-
25
- Load it in your code to start building quadtrees:
26
-
27
- ```ruby
28
- require 'quadtree'
29
-
30
- boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
31
- qt = Quadtree::Quadtree.new(boundary)
32
- ```
33
-
34
- Then you can do lookups and such:
35
-
36
- ```ruby
37
- getaboden = Quadtree::Point.new(19.8470050, 60.3747940, "Getaboden")
38
- knutnas = Quadtree::Point.new(19.8271170, 60.3505570, "Knutnäs")
39
- boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
40
- boundary2 = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 4472.0)
41
- qt.insert! getaboden
42
- qt.insert! knutnas
43
- qt.query_range(boundary2)
44
- # [#<Quadtree::Point:0x00007fdcb19e0698 @data="Getaboden", @x=19.847005, @y=60.374794>,
45
- #<Quadtree::Point:0x00007fdcb19ec7b8 @data="Knutnäs", @x=19.827117, @y=60.350557>]
46
- ```
47
-
48
- ## Development
49
-
50
- 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.
51
-
52
- 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).
53
-
54
- ## Contributing
55
-
56
- 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.
57
-
58
- ## License
59
-
60
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
61
-
62
- ## Code of Conduct
63
-
64
- 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
+ [![Gem Version](https://badge.fury.io/rb/quadtree.svg)](https://badge.fury.io/rb/quadtree)
4
+ ![Build Status](https://codebuild.us-east-1.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoibFBLaWRWODEwbjl4dWV1UWdFUDlKa3dqVkZOb3RJTytBRHg5WVU3bGV6TUdTSjFRSWtRYnVZdC9zWkVvUldTdkMwOFQ2cU8rdGQ4dzFBeFN3UE9jR1g4PSIsIml2UGFyYW1ldGVyU3BlYyI6IkJUSU0rdFJhWXRIbDVzckciLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master)
5
+
6
+ Quadtrees in Ruby. For searching spatially related nodes in some space, you know.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'quadtree'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install quadtree
23
+
24
+ ## Usage
25
+
26
+ Load it in your code to start building quadtrees:
27
+
28
+ ```ruby
29
+ require 'quadtree'
30
+
31
+ boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
32
+ qt = Quadtree::Quadtree.new(boundary)
33
+ ```
34
+
35
+ Then you can do lookups and such:
36
+
37
+ ```ruby
38
+ getaboden = Quadtree::Point.new(19.8470050, 60.3747940, "Getaboden")
39
+ knutnas = Quadtree::Point.new(19.8271170, 60.3505570, "Knutnäs")
40
+ boundary = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 8944.0)
41
+ boundary2 = Quadtree::AxisAlignedBoundingBox.new(Quadtree::Point.new(19.8470050, 60.3747940), 4472.0)
42
+ qt.insert! getaboden
43
+ qt.insert! knutnas
44
+ qt.query_range(boundary2)
45
+ # [#<Quadtree::Point:0x00007fdcb19e0698 @data="Getaboden", @x=19.847005, @y=60.374794>,
46
+ #<Quadtree::Point:0x00007fdcb19ec7b8 @data="Knutnäs", @x=19.827117, @y=60.350557>]
47
+ ```
48
+
49
+ ## Development
50
+
51
+ 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.
52
+
53
+ 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).
54
+
55
+ ## Contributing
56
+
57
+ 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.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ 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 CHANGED
@@ -1,13 +1,55 @@
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
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 "rubygems"
7
+ #require "rubygems/package_task"
8
+ require "rspec/core/rake_task"
9
+ require "yard"
10
+ require "yard/rake/yardoc_task"
11
+ require "quadtree"
12
+
13
+ RSpec::Core::RakeTask.new(:spec)
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.name = "quadtree"
17
+ s.version = Quadtree::VERSION
18
+ s.authors = ["Jan Lindblom"]
19
+ s.email = ["janlindblom@fastmail.fm"]
20
+
21
+ s.summary = %q{Quadtrees in Ruby.}
22
+ s.homepage = "https://bitbucket.org/janlindblom/ruby-quadtree"
23
+ s.license = "MIT"
24
+
25
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(bin|test|spec|features)/}) ||
27
+ f == ".travis.yml" ||
28
+ f == "bitbucket-pipelines.yml" ||
29
+ f == "buildspec.yml"
30
+ end
31
+ s.bindir = "exe"
32
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ s.require_paths = ["lib"]
34
+
35
+ s.required_ruby_version = '>= 2.3.0'
36
+
37
+ s.add_development_dependency "bundler", "~> 1.14"
38
+ s.add_development_dependency "rake", "~> 10.0"
39
+ s.add_development_dependency "rspec", "~> 3.0"
40
+ s.add_development_dependency "pry", "~> 0.11"
41
+ s.add_development_dependency "yard", "~> 0.9"
42
+ s.add_runtime_dependency "version", "~> 1.1"
43
+ end
44
+
45
+ Rake::VersionTask.new do |task|
46
+ task.with_gemspec = spec
47
+ task.with_git = false
48
+ end
49
+
50
+ YARD::Rake::YardocTask.new do |t|
51
+ t.files = ['lib/**/*.rb']
52
+ t.stats_options = ['--list-undoc']
53
+ end
54
+
55
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.6
data/lib/quadtree.rb CHANGED
@@ -1,12 +1,14 @@
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
- # @author Jan Lindblom <janlindblom@fastmail.fm>
10
- module Quadtree
11
-
12
- end
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,108 +1,108 @@
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
+ 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