Class: Metro::Units::RectangleBounds

Inherits:
Object
  • Object
show all
Includes:
CalculationValidations
Defined in:
lib/metro/units/rectangle_bounds.rb

Overview

An object that represents a rectanglar bounds.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CalculationValidations

#*, #+, #-, #calculate, #check_calculation_requirements

Constructor Details

#initialize(params = {}) ⇒ RectangleBounds

Create a bounds with bounds.



19
20
21
22
23
24
# File 'lib/metro/units/rectangle_bounds.rb', line 19

def initialize(params = {})
  @left = params[:left].to_f
  @top = params[:top].to_f
  @right = params[:right].to_f
  @bottom = params[:bottom].to_f
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



10
11
12
# File 'lib/metro/units/rectangle_bounds.rb', line 10

def bottom
  @bottom
end

#leftObject

Returns the value of attribute left.



10
11
12
# File 'lib/metro/units/rectangle_bounds.rb', line 10

def left
  @left
end

#rightObject

Returns the value of attribute right.



10
11
12
# File 'lib/metro/units/rectangle_bounds.rb', line 10

def right
  @right
end

#topObject

Returns the value of attribute top.



10
11
12
# File 'lib/metro/units/rectangle_bounds.rb', line 10

def top
  @top
end

Class Method Details

.noneObject



12
13
14
# File 'lib/metro/units/rectangle_bounds.rb', line 12

def self.none
  new left: 0, right: 0, top: 0, bottom: 0
end

Instance Method Details

#==(value) ⇒ Object



53
54
55
56
# File 'lib/metro/units/rectangle_bounds.rb', line 53

def ==(value)
  check_calculation_requirements(value)
  left == value.left and right == value.right and top == value.top and bottom == value.bottom
end

#bottom_leftObject



45
46
47
# File 'lib/metro/units/rectangle_bounds.rb', line 45

def bottom_left
  Point.at(left,bottom)
end

#bottom_rightObject



41
42
43
# File 'lib/metro/units/rectangle_bounds.rb', line 41

def bottom_right
  Point.at(right,bottom)
end

#contains?(point) ⇒ Boolean

Does this bounds contain the following point?

Returns:

  • (Boolean)


61
62
63
# File 'lib/metro/units/rectangle_bounds.rb', line 61

def contains?(point)
  point.x > left and point.x < right and point.y > top and point.y < bottom
end

#dimensionsObject



49
50
51
# File 'lib/metro/units/rectangle_bounds.rb', line 49

def dimensions
  Dimensions.of (right - left), (bottom - top)
end

#intersect?(b) ⇒ Boolean

Does this rectanglular bounds intersect with another rectanglular bounds?

Returns:

  • (Boolean)


68
69
70
# File 'lib/metro/units/rectangle_bounds.rb', line 68

def intersect?(b)
  not(b.left > right or b.right < left or b.top > bottom or b.bottom < top)
end

#shift(point) ⇒ Object



26
27
28
29
30
31
# File 'lib/metro/units/rectangle_bounds.rb', line 26

def shift(point)
  self.left = self.left + point.x
  self.right = self.right + point.x
  self.top = self.top + point.y
  self.bottom = self.bottom + point.y
end

#to_sObject



72
73
74
# File 'lib/metro/units/rectangle_bounds.rb', line 72

def to_s
  "(#{left},#{top}) to (#{right},#{bottom})"
end

#top_leftObject



33
34
35
# File 'lib/metro/units/rectangle_bounds.rb', line 33

def top_left
  Point.at(left,top)
end

#top_rightObject



37
38
39
# File 'lib/metro/units/rectangle_bounds.rb', line 37

def top_right
  Point.at(right,top)
end