Class: OrangeZest::Box
- Inherits:
-
Object
- Object
- OrangeZest::Box
- Defined in:
- lib/orange_zest/box.rb
Overview
A box, with an origin in the top-left corner.
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#centre ⇒ Point
(also: #center)
Returns the point at the centre of this box.
-
#initialize(origin, width, height) ⇒ Box
constructor
A new instance of Box.
-
#overlaps?(other) ⇒ Boolean
Returns true if this box overlaps another box.
-
#point_inside?(point) ⇒ Boolean
Returns true if a point is inside this box.
Constructor Details
#initialize(origin, width, height) ⇒ Box
Returns a new instance of Box.
4 5 6 7 8 |
# File 'lib/orange_zest/box.rb', line 4 def initialize(origin, width, height) @origin = origin @width = width @height = height end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
10 11 12 |
# File 'lib/orange_zest/box.rb', line 10 def height @height end |
#origin ⇒ Object
Returns the value of attribute origin.
10 11 12 |
# File 'lib/orange_zest/box.rb', line 10 def origin @origin end |
#width ⇒ Object
Returns the value of attribute width.
10 11 12 |
# File 'lib/orange_zest/box.rb', line 10 def width @width end |
Instance Method Details
#centre ⇒ Point Also known as: center
Returns the point at the centre of this box.
29 30 31 |
# File 'lib/orange_zest/box.rb', line 29 def centre origin + Point.new(width / 2, height / 2) end |
#overlaps?(other) ⇒ Boolean
Returns true if this box overlaps another box.
13 14 15 16 17 18 |
# File 'lib/orange_zest/box.rb', line 13 def overlaps?(other) self.origin.x < other.origin.x + other.width \ && other.origin.x < self.origin.x + self.width \ && self.origin.y < other.origin.y + other.height \ && other.origin.y < self.origin.y + self.height end |
#point_inside?(point) ⇒ Boolean
Returns true if a point is inside this box.
22 23 24 25 |
# File 'lib/orange_zest/box.rb', line 22 def point_inside?(point) point.x >= origin.x && point.x <= origin.x + width \ && point.y >= origin.y && point.y <= origin.y + height end |