Class: OrangeZest::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/orange_zest/box.rb

Overview

A box, with an origin in the top-left corner.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#heightObject

Returns the value of attribute height.



10
11
12
# File 'lib/orange_zest/box.rb', line 10

def height
  @height
end

#originObject

Returns the value of attribute origin.



10
11
12
# File 'lib/orange_zest/box.rb', line 10

def origin
  @origin
end

#widthObject

Returns the value of attribute width.



10
11
12
# File 'lib/orange_zest/box.rb', line 10

def width
  @width
end

Instance Method Details

#centrePoint Also known as: center

Returns the point at the centre of this box.

Returns:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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