Class: Applitools::Region

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Region.



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

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/core/region.rb', line 3

def height
  @height
end

#leftObject Also known as: x

Returns the value of attribute left.



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

def left
  @left
end

#topObject Also known as: y

Returns the value of attribute top.



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

def top
  @top
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

Class Method Details

.from_location_size(location, size) ⇒ Object



8
9
10
# File 'lib/applitools/core/region.rb', line 8

def from_location_size(location, size)
  new location.x, location.y, size.width, size.height
end

.sub_regions_with_fixed_size(container_region, sub_region) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/applitools/core/region.rb', line 110

def sub_regions_with_fixed_size(container_region, sub_region)
  Applitools::ArgumentGuard.not_nil container_region, 'container_region'
  Applitools::ArgumentGuard.not_nil sub_region, 'sub_region'

  Applitools::ArgumentGuard.greater_than_zero(sub_region.width, 'sub_region.width')
  Applitools::ArgumentGuard.greater_than_zero(sub_region.height, 'sub_region.height')

  sub_region_width = sub_region.width
  sub_region_height = sub_region.height

  # Normalizing.
  sub_region_width = container_region.width if sub_region_width > container_region.width
  sub_region_height = container_region.height if sub_region_height > container_region.height

  if sub_region_width == container_region.width && sub_region_height == container_region.height
    return Enumerator(1) do |y|
      y << sub_region
    end
  end

  current_top = container_region.top
  bottom = container_region.top + container_region.height - 1
  right = container_region.left + container_region.width - 1
  Enumerator.new do |y|
    while current_top <= bottom
      current_top = (bottom - sub_region_height) + 1 if current_top + sub_region_height > bottom
      current_left = container_region.left
      while current_left <= right
        current_left = (rught - sub_region_width) + 1 if current_left + sub_region_width > right
        y << new(current_left, current_top, sub_region_width, sub_region_height)
        current_left += sub_region_width
      end
      current_top += sub_region_height
    end
  end
end

.sub_regions_with_varying_size(container_region, sub_region) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/applitools/core/region.rb', line 147

def sub_regions_with_varying_size(container_region, sub_region)
  Applitools::ArgumentGuard.not_nil container_region, 'container_region'
  Applitools::ArgumentGuard.not_nil sub_region, 'sub_region'

  Applitools::ArgumentGuard.greater_than_zero(sub_region.width, 'sub_region.width')
  Applitools::ArgumentGuard.greater_than_zero(sub_region.height, 'sub_region.height')

  current_top = container_region.top
  bottom = container_region.top + container_region.height
  right = container_region.left + container_region.width

  Enumerator.new do |y|
    while current_top < bottom
      current_bottom = current_top + sub_region.height
      current_bottom = bottom if current_bottom > bottom
      current_left = container_region.left
      while current_left < right
        current_right = current_left + sub_region.width
        current_right = right if current_right > right

        current_height = current_bottom - current_top
        current_width = current_right - current_left

        y << new(current_left, current_top, current_width, current_height)

        current_left += sub_region.width
      end
      current_top += sub_region.height
    end
  end
end

Instance Method Details

#bottomObject



50
51
52
# File 'lib/applitools/core/region.rb', line 50

def bottom
  top + height
end

#contains?(other_left, other_top) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/applitools/core/region.rb', line 77

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#intersect(other) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/applitools/core/region.rb', line 59

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)


54
55
56
57
# File 'lib/applitools/core/region.rb', line 54

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

#locationObject



37
38
39
# File 'lib/applitools/core/region.rb', line 37

def location
  Location.new left, top
end

#location=(other_location) ⇒ Object



41
42
43
44
# File 'lib/applitools/core/region.rb', line 41

def location=(other_location)
  self.left = other_location.left
  self.top = other_location.top
end

#make_emptyObject



22
23
24
25
26
27
# File 'lib/applitools/core/region.rb', line 22

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

#middle_offsetObject



81
82
83
84
85
# File 'lib/applitools/core/region.rb', line 81

def middle_offset
  mid_x = width / 2
  mid_y = height / 2
  Applitools::Location.for(mid_x.round, mid_y.round)
end

#rightObject



46
47
48
# File 'lib/applitools/core/region.rb', line 46

def right
  left + width
end

#sizeObject



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

def size
  Applitools::RectangleSize.new width, height
end

#size_equals?(region) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/applitools/core/region.rb', line 105

def size_equals?(region)
  width == region.width && height == region.height
end

#sub_regions(subregion_size, is_fixed_size = false) ⇒ Object



87
88
89
90
# File 'lib/applitools/core/region.rb', line 87

def sub_regions(subregion_size, is_fixed_size = false)
  return self.class.sub_regions_with_fixed_size self, subregion_size if is_fixed_size
  self.class.sub_regions_with_varying_size self, subregion_size
end

#to_hashObject



92
93
94
95
96
97
98
99
# File 'lib/applitools/core/region.rb', line 92

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

#to_sObject



101
102
103
# File 'lib/applitools/core/region.rb', line 101

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