Class: SnapDiff::Region
- Inherits:
-
Object
- Object
- SnapDiff::Region
- Defined in:
- lib/snap_diff/region.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
need to add this method to make it work with assert_equal.
- #blank? ⇒ Boolean
- #bottom ⇒ Object
- #cover?(x, y) ⇒ Boolean
- #empty? ⇒ Boolean
- #find_intersect_with(region) ⇒ Object
- #find_relative_intersect(region) ⇒ Object
-
#initialize(x, y, width, height) ⇒ Region
constructor
A new instance of Region.
- #inspect ⇒ Object
- #intersect?(region) ⇒ Boolean
- #left ⇒ Object
- #move_by(right_by, down_by) ⇒ Object
- #present? ⇒ Boolean
- #right ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_edge_coordinates ⇒ Object
- #to_top_left_corner_coordinates ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(x, y, width, height) ⇒ Region
Returns a new instance of Region.
7 8 9 |
# File 'lib/snap_diff/region.rb', line 7 def initialize(x, y, width, height) @x, @y, @width, @height = x, y, width, height end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
5 6 7 |
# File 'lib/snap_diff/region.rb', line 5 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
5 6 7 |
# File 'lib/snap_diff/region.rb', line 5 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/snap_diff/region.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/snap_diff/region.rb', line 5 def y @y end |
Class Method Details
.from_edge_coordinates(left, top, right, bottom) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/snap_diff/region.rb', line 11 def self.from_edge_coordinates(left, top, right, bottom) return nil unless left && top && right && bottom return nil if right < left || bottom < top Region.new(left, top, right - left, bottom - top) end |
Instance Method Details
#==(other) ⇒ Object
need to add this method to make it work with assert_equal
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/snap_diff/region.rb', line 98 def ==(other) case other when Region x == other.x && y == other.y && width == other.width && height == other.height when Array to_a == other else false end end |
#blank? ⇒ Boolean
85 86 87 |
# File 'lib/snap_diff/region.rb', line 85 def blank? empty? end |
#bottom ⇒ Object
30 31 32 |
# File 'lib/snap_diff/region.rb', line 30 def bottom y + height end |
#cover?(x, y) ⇒ Boolean
77 78 79 |
# File 'lib/snap_diff/region.rb', line 77 def cover?(x, y) x.between?(left, right) && y.between?(top, bottom) end |
#empty? ⇒ Boolean
81 82 83 |
# File 'lib/snap_diff/region.rb', line 81 def empty? width.zero? || height.zero? end |
#find_intersect_with(region) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/snap_diff/region.rb', line 53 def find_intersect_with(region) return nil unless intersect?(region) new_left = [x, region.x].max new_top = [y, region.y].max Region.new(new_left, new_top, [right, region.right].min - new_left, [bottom, region.bottom].min - new_top) end |
#find_relative_intersect(region) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/snap_diff/region.rb', line 70 def find_relative_intersect(region) intersect = find_intersect_with(region) return nil unless intersect intersect.move_by(-x, -y) end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/snap_diff/region.rb', line 93 def inspect "Region(x: #{x}, y: #{y}, width: #{width}, height: #{height})" end |
#intersect?(region) ⇒ Boolean
62 63 64 |
# File 'lib/snap_diff/region.rb', line 62 def intersect?(region) left <= region.right && right >= region.left && top <= region.bottom && bottom >= region.top end |
#left ⇒ Object
34 35 36 |
# File 'lib/snap_diff/region.rb', line 34 def left x end |
#move_by(right_by, down_by) ⇒ Object
66 67 68 |
# File 'lib/snap_diff/region.rb', line 66 def move_by(right_by, down_by) Region.new(x + right_by, y + down_by, width, height) end |
#present? ⇒ Boolean
89 90 91 |
# File 'lib/snap_diff/region.rb', line 89 def present? !empty? end |
#right ⇒ Object
38 39 40 |
# File 'lib/snap_diff/region.rb', line 38 def right x + width end |
#size ⇒ Object
42 43 44 45 46 47 |
# File 'lib/snap_diff/region.rb', line 42 def size return 0 if width < 0 || height < 0 result = width * height result.zero? ? 1 : result end |
#to_a ⇒ Object
49 50 51 |
# File 'lib/snap_diff/region.rb', line 49 def to_a [@x, @y, @width, @height] end |
#to_edge_coordinates ⇒ Object
18 19 20 |
# File 'lib/snap_diff/region.rb', line 18 def to_edge_coordinates [left, top, right, bottom] end |
#to_top_left_corner_coordinates ⇒ Object
22 23 24 |
# File 'lib/snap_diff/region.rb', line 22 def to_top_left_corner_coordinates [x, y, width, height] end |
#top ⇒ Object
26 27 28 |
# File 'lib/snap_diff/region.rb', line 26 def top y end |