Class: Percept::Line

Inherits:
Object
  • Object
show all
Includes:
ColorUtils, Utils
Defined in:
lib/percept/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorUtils

#color!

Methods included from Utils

#color_tolerance, #scaled_color

Constructor Details

#initialize(params) ⇒ Line

Returns a new instance of Line.



11
12
13
14
15
16
17
18
# File 'lib/percept/line.rb', line 11

def initialize(params)
  self.start_x = params.fetch(:start_x)
  self.end_x = params.fetch(:end_x)
  self.start_y = params.fetch(:start_y)
  self.end_y = params.fetch(:end_y) { start_y + 1 }
  self.rows = params.fetch(:rows) { [params.fetch(:row)] }
  self.image = params.fetch(:image)
end

Instance Attribute Details

#end_xObject

Returns the value of attribute end_x.



8
9
10
# File 'lib/percept/line.rb', line 8

def end_x
  @end_x
end

#end_yObject

Returns the value of attribute end_y.



9
10
11
# File 'lib/percept/line.rb', line 9

def end_y
  @end_y
end

#imageObject

Returns the value of attribute image.



8
9
10
# File 'lib/percept/line.rb', line 8

def image
  @image
end

#rowsObject

Returns the value of attribute rows.



8
9
10
# File 'lib/percept/line.rb', line 8

def rows
  @rows
end

#start_xObject

Returns the value of attribute start_x.



8
9
10
# File 'lib/percept/line.rb', line 8

def start_x
  @start_x
end

#start_yObject

Returns the value of attribute start_y.



8
9
10
# File 'lib/percept/line.rb', line 8

def start_y
  @start_y
end

Instance Method Details

#=~(other) ⇒ Object



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

def =~(other)
  overlaps_x?(other) && overlaps_y?(other)
end

#attributesObject



49
50
51
52
53
54
55
56
# File 'lib/percept/line.rb', line 49

def attributes
  {
    start_x: start_x,
    end_x: end_x,
    start_y: start_y,
    end_y: end_y,
  }
end

#columns_for(x_range, start_y, row_count = 2) ⇒ Object



93
94
95
96
97
98
# File 'lib/percept/line.rb', line 93

def columns_for(x_range, start_y, row_count = 2)
  x_range.map do |x|
    next if x > image.column_count || start_y + row_count > image.row_count
    image.get_pixels(x, start_y, 1, row_count)
  end.compact
end

#copy_line(params) ⇒ Object



89
90
91
# File 'lib/percept/line.rb', line 89

def copy_line(params)
  Line.new(full_attributes.merge(params))
end

#full_attributesObject



58
59
60
# File 'lib/percept/line.rb', line 58

def full_attributes
  attributes.merge(rows: rows, image: image)
end

#heightObject



66
67
68
# File 'lib/percept/line.rb', line 66

def height
  end_y - start_y
end

#inspectObject



45
46
47
# File 'lib/percept/line.rb', line 45

def inspect
  to_s
end

#intersectionsObject



100
101
102
103
104
105
106
107
# File 'lib/percept/line.rb', line 100

def intersections
  above_columns = columns_for(start_x..end_x, start_y - 4, 4)
  vertical_dividers = []
  above_columns.each_with_index do |column, index|
    vertical_dividers << index if column.any?(&:blackish?)
  end
  vertical_dividers.map { |x| x + start_x }
end

#lengthObject



62
63
64
# File 'lib/percept/line.rb', line 62

def length
  end_x - start_x
end

#merge!(other) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/percept/line.rb', line 109

def merge!(other)
  if start_y > other.start_y
    self.start_y = other.start_y
  elsif end_y < other.end_y
    self.end_y = other.end_y
  end

  self.start_x = other.start_x if start_x > other.start_x
  self.end_x = other.end_x if end_x < other.end_x

  self.rows += other.rows
end

#overlaps_x?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def overlaps_x?(other)
  !(end_x < other.start_x || start_x > other.end_x)
end

#overlaps_y?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def overlaps_y?(other)
  !(end_y < other.start_y || start_y > other.end_y)
end

#pixelsObject



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

def pixels
  @pixels ||= rows.flat_map { |row| row[start_x...end_x] }
end

#short?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/percept/line.rb', line 122

def short?
  length < Percept.config.line_length
end

#splitObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/percept/line.rb', line 70

def split
  lines = LineCollection.new
  next_x = start_x
  intersections.each do |x_coordinate|
    if x_coordinate == next_x
      next_x += 1
      next
    end
    line = copy_line(start_x: next_x, end_x: x_coordinate)
    next_x = x_coordinate + 1
    lines << line unless line.short?
  end
  if next_x < end_x
    line = copy_line(start_x: next_x)
    lines << line unless line.short?
  end
  lines
end

#store_pixels!Object



126
127
128
# File 'lib/percept/line.rb', line 126

def store_pixels!
  image.store_pixels(start_x, start_y, length, height, pixels)
end

#to_sObject



41
42
43
# File 'lib/percept/line.rb', line 41

def to_s
  attributes.to_s
end