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



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

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

#bottom_leftObject



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

def bottom_left
  Point.at(left,bottom)
end

#bottom_rightObject



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

def bottom_right
  Point.at(right,bottom)
end

#contains?(point) ⇒ Boolean

Does this bounds contain the following point?

Returns:

  • (Boolean)


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

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

#dimensionsObject



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

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

#intersect?(b) ⇒ Boolean

Does this rectanglular bounds intersect with another rectanglular bounds?

Returns:

  • (Boolean)


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

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

#to_sObject



65
66
67
# File 'lib/metro/units/rectangle_bounds.rb', line 65

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

#top_leftObject



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

def top_left
  Point.at(left,top)
end

#top_rightObject



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

def top_right
  Point.at(right,top)
end