Class: Applitools::Base::Region

Inherits:
Object
  • Object
show all
Defined in:
lib/applitools/base/region.rb

Constant Summary collapse

EMPTY =
Region.new(0, 0, 0, 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, top, width, height) ⇒ Region

Returns a new instance of Region.



5
6
7
8
9
10
# File 'lib/applitools/base/region.rb', line 5

def initialize(left, top, width, height)
  @left = left.round
  @top = top.round
  @width = width.round
  @height = height.round
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/applitools/base/region.rb', line 3

def height
  @height
end

#leftObject

Returns the value of attribute left.



3
4
5
# File 'lib/applitools/base/region.rb', line 3

def left
  @left
end

#topObject

Returns the value of attribute top.



3
4
5
# File 'lib/applitools/base/region.rb', line 3

def top
  @top
end

#widthObject

Returns the value of attribute width.



3
4
5
# File 'lib/applitools/base/region.rb', line 3

def width
  @width
end

Instance Method Details

#bottomObject



29
30
31
# File 'lib/applitools/base/region.rb', line 29

def bottom
  top + height
end

#contains?(other_left, other_top) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/applitools/base/region.rb', line 56

def contains?(other_left, other_top)
  other_left >= left && other_left <= right && other_top >= top && other_top <= bottom
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/applitools/base/region.rb', line 21

def empty?
  @left == EMPTY.left && @top == EMPTY.top && @width == EMPTY.width && @height == EMPTY.height
end

#intersect(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/applitools/base/region.rb', line 38

def intersect(other)
  unless intersecting?(other)
    make_empty

    return
  end

  i_left = (left >= other.left) ? left : other.left
  i_right = (right <= other.right) ? right : other.right
  i_top = (top >= other.top) ? top : other.top
  i_bottom = (bottom <= other.bottom) ? bottom : other.bottom

  @left = i_left
  @top = i_top
  @width = i_right - i_left
  @height = i_bottom - i_top
end

#intersecting?(other) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/applitools/base/region.rb', line 33

def intersecting?(other)
  ((left <= other.left && other.left <= right) || (other.left <= left && left <= other.right)) &&
    ((top <= other.top && other.top <= bottom) || (other.top <= top && top <= other.bottom))
end

#make_emptyObject



14
15
16
17
18
19
# File 'lib/applitools/base/region.rb', line 14

def make_empty
  @left = EMPTY.left
  @top = EMPTY.top
  @width = EMPTY.width
  @height = EMPTY.height
end

#middle_offsetObject



60
61
62
63
64
# File 'lib/applitools/base/region.rb', line 60

def middle_offset
  mid_x = width / 2
  mid_y = height / 2
  Point.new(mid_x.round, mid_y.round)
end

#rightObject



25
26
27
# File 'lib/applitools/base/region.rb', line 25

def right
  left + width
end

#subregions(subregion_size) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/applitools/base/region.rb', line 66

def subregions(subregion_size)
  [].tap do |subregions|
    current_top = @top
    bottom = @top + @height
    right = @left + @width
    subregion_width = [@width, subregion_size.width].min
    subregion_height = [@height, subregion_size.height].min

    while current_top < bottom
      current_bottom = current_top + subregion_height
      if current_bottom > bottom
        current_bottom = bottom
        current_top = current_bottom - subregion_height
      end

      current_left = @left
      while current_left < right
        current_right = current_left + subregion_width
        if current_right > right
          current_right = right
          current_left = current_right - subregion_width
        end

        subregions << Region.new(current_left, current_top, subregion_width, subregion_height)

        current_left += subregion_width
      end

      current_top += subregion_height
    end
  end
end

#to_hashObject



99
100
101
102
103
104
105
106
# File 'lib/applitools/base/region.rb', line 99

def to_hash
  {
    left: left,
    top: top,
    height: height,
    width: width
  }
end

#to_sObject



108
109
110
# File 'lib/applitools/base/region.rb', line 108

def to_s
  "(#{left}, #{top}), #{width} x #{height}"
end