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



112
113
114
115
# File 'lib/metro/units/rectangle_bounds.rb', line 112

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

#bottom_leftObject



92
93
94
# File 'lib/metro/units/rectangle_bounds.rb', line 92

def bottom_left
  point_at left, bottom
end

#bottom_rightObject



88
89
90
# File 'lib/metro/units/rectangle_bounds.rb', line 88

def bottom_right
  point_at right, bottom
end

#centerObject



104
105
106
# File 'lib/metro/units/rectangle_bounds.rb', line 104

def center
  top_left + point_at(width/2,height/2)
end

#clamp(point) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metro/units/rectangle_bounds.rb', line 56

def clamp(point)
  return point if contains?(point)

  new_point = Point.new(point.x,point.y)

  if point.x < left
    new_point.x = left
  end

  if point.x > right
    new_point.x = right
  end

  if point.y < top
    new_point.y = top
  end

  if point.y > bottom
    new_point.y = bottom
  end

  new_point
end

#contains?(point) ⇒ Boolean

Does this bounds contain the following point?

Returns:

  • (Boolean)


120
121
122
# File 'lib/metro/units/rectangle_bounds.rb', line 120

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

#dimensionsObject



108
109
110
# File 'lib/metro/units/rectangle_bounds.rb', line 108

def dimensions
  Dimensions.of width, height
end

#enlarge(params = {}) ⇒ Object

Create a new RectangleBounds that is larger than the current bounds, given the specifed hash of parameters that contain the amount to expand in the four directions left, right, top, bottom.



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

def enlarge(params = {})
  self.class.new(left: left - params[:left].to_i,
      right: right + params[:right].to_i,
      top: top - params[:top].to_i,
      bottom: bottom + params[:bottom].to_i)
end

#heightObject



100
101
102
# File 'lib/metro/units/rectangle_bounds.rb', line 100

def height
  (bottom - top)
end

#intersect?(b) ⇒ Boolean

Does this rectanglular bounds intersect with another rectanglular bounds?

Returns:

  • (Boolean)


127
128
129
# File 'lib/metro/units/rectangle_bounds.rb', line 127

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

#shift(point) ⇒ Object

Move the bounds by the amount specified in the point.



29
30
31
32
33
34
# File 'lib/metro/units/rectangle_bounds.rb', line 29

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

#shrink(params = {}) ⇒ Object



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

def shrink(params = {})
  self.class.new(left: left + params[:left].to_i,
      right: right - params[:right].to_i,
      top: top + params[:top].to_i,
      bottom: bottom - params[:bottom].to_i)
end

#to_sObject



131
132
133
# File 'lib/metro/units/rectangle_bounds.rb', line 131

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

#top_leftObject



80
81
82
# File 'lib/metro/units/rectangle_bounds.rb', line 80

def top_left
  point_at left, top
end

#top_rightObject



84
85
86
# File 'lib/metro/units/rectangle_bounds.rb', line 84

def top_right
  point_at right, top
end

#widthObject



96
97
98
# File 'lib/metro/units/rectangle_bounds.rb', line 96

def width
  (right - left)
end