Class: Qrio::Region

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

Overview

a rectangular matrix of bits

Direct Known Subclasses

FinderPatternSlice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x1, y1, x2, y2) ⇒ Region

Returns a new instance of Region.



6
7
8
9
10
11
12
13
# File 'lib/qrio/region.rb', line 6

def initialize(x1, y1, x2, y2)
  @x1 = x1
  @y1 = y1
  @x2 = x2
  @y2 = y2

  set_orientation
end

Instance Attribute Details

#orientationObject (readonly)

Returns the value of attribute orientation.



4
5
6
# File 'lib/qrio/region.rb', line 4

def orientation
  @orientation
end

#x1Object (readonly)

Returns the value of attribute x1.



4
5
6
# File 'lib/qrio/region.rb', line 4

def x1
  @x1
end

#x2Object (readonly)

Returns the value of attribute x2.



4
5
6
# File 'lib/qrio/region.rb', line 4

def x2
  @x2
end

#y1Object (readonly)

Returns the value of attribute y1.



4
5
6
# File 'lib/qrio/region.rb', line 4

def y1
  @y1
end

#y2Object (readonly)

Returns the value of attribute y2.



4
5
6
# File 'lib/qrio/region.rb', line 4

def y2
  @y2
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
# File 'lib/qrio/region.rb', line 48

def ==(other)
  to_s == other.to_s
end

#bottomObject



18
# File 'lib/qrio/region.rb', line 18

def bottom; y2; end

#bottom_rightObject



24
25
26
# File 'lib/qrio/region.rb', line 24

def bottom_right
  [x2, y2]
end

#centerObject



60
61
62
# File 'lib/qrio/region.rb', line 60

def center
  [left + width / 2, top + height / 2]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/qrio/region.rb', line 44

def eql?(other)
  self == other
end

#hashObject



40
41
42
# File 'lib/qrio/region.rb', line 40

def hash
  to_s.hash
end

#heightObject



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

def height
  y2 - y1 + 1
end

#horizontal?Boolean

Returns:

  • (Boolean)


64
# File 'lib/qrio/region.rb', line 64

def horizontal?; orientation == :horizontal; end

#leftObject



15
# File 'lib/qrio/region.rb', line 15

def left;   x1; end

#orientation_matches?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/qrio/region.rb', line 67

def orientation_matches?(other)
  orientation == other.orientation
end

#rightObject



16
# File 'lib/qrio/region.rb', line 16

def right;  x2; end

#rotate(mw, mh) ⇒ Object

return a new region that would be the result of rotating a matrix of width x height containing this region



91
92
93
94
95
96
97
98
# File 'lib/qrio/region.rb', line 91

def rotate(mw, mh)
  self.class.new(
    mh - bottom - 1,
    left,
    mh - top - 1,
    right
  )
end

#to_coordinatesObject



28
29
30
# File 'lib/qrio/region.rb', line 28

def to_coordinates
  [top_left, bottom_right].flatten
end

#to_point_sizeObject



32
33
34
# File 'lib/qrio/region.rb', line 32

def to_point_size
  [top_left, width, height].flatten
end

#to_sObject



36
37
38
# File 'lib/qrio/region.rb', line 36

def to_s
  "R[#{ to_coordinates.join(',') }]"
end

#topObject



17
# File 'lib/qrio/region.rb', line 17

def top;    y1; end

#top_leftObject



20
21
22
# File 'lib/qrio/region.rb', line 20

def top_left
  [x1, y1]
end

#translate(xoffset, yoffset) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/qrio/region.rb', line 80

def translate(xoffset, yoffset)
  self.class.new(
    left   - xoffset,
    top    - yoffset,
    right  - xoffset,
    bottom - yoffset
  )
end

#union(other) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/qrio/region.rb', line 71

def union(other)
  self.class.new(
    [left,   other.left].min,
    [top,    other.top].min,
    [right,  other.right].max,
    [bottom, other.bottom].max
  )
end

#vertical?Boolean

Returns:

  • (Boolean)


65
# File 'lib/qrio/region.rb', line 65

def vertical?;   orientation == :vertical;   end

#widthObject



52
53
54
# File 'lib/qrio/region.rb', line 52

def width
  x2 - x1 + 1
end