Class: BitmapCompiler::Core::Bitmap
- Inherits:
-
Object
- Object
- BitmapCompiler::Core::Bitmap
- Defined in:
- lib/bitmap_compiler/core/bitmap.rb
Constant Summary collapse
- MIN_WIDTH =
MIN_HEIGHT = 1
- MAX_WIDTH =
MAX_HEIGHT = 250
- STANDARD_COLOR =
'O'.freeze
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #change_pixel(row, column, color) ⇒ Object
- #clear ⇒ Object
-
#initialize(width, height, cells: nil) ⇒ Bitmap
constructor
A new instance of Bitmap.
- #initialize_matrix ⇒ Object
- #print ⇒ Object
- #valid_column_index?(column) ⇒ Boolean
- #valid_coordinate?(row, column) ⇒ Boolean
- #valid_horizontal_line_coordinates?(column, start_row, end_row) ⇒ Boolean
- #valid_row_index?(row) ⇒ Boolean
- #valid_vertical_line_coordinates?(row, start_column, end_column) ⇒ Boolean
Constructor Details
#initialize(width, height, cells: nil) ⇒ Bitmap
Returns a new instance of Bitmap.
10 11 12 13 14 15 16 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 10 def initialize(width, height, cells: nil) @height = Integer(height) @width = Integer(width) @cells = cells clear if cells.nil? end |
Instance Attribute Details
#cells ⇒ Object (readonly)
Returns the value of attribute cells.
8 9 10 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 8 def cells @cells end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 8 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 8 def width @width end |
Instance Method Details
#change_pixel(row, column, color) ⇒ Object
18 19 20 21 22 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 18 def change_pixel(row, column, color) return false unless valid_coordinate?(row, column) cells[row - 1][column - 1] = color end |
#clear ⇒ Object
24 25 26 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 24 def clear @cells = initialize_matrix end |
#initialize_matrix ⇒ Object
28 29 30 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 28 def initialize_matrix Array.new(width) { Array.new(height) { STANDARD_COLOR } } end |
#print ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 32 def print ''.tap do |output| (0..cells[0].length - 1).each do |column_index| (0..cells.length - 1).each do |row_index| output << cells[row_index][column_index] end output << "\n" end end end |
#valid_column_index?(column) ⇒ Boolean
58 59 60 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 58 def valid_column_index?(column) column.between?(1, height) end |
#valid_coordinate?(row, column) ⇒ Boolean
54 55 56 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 54 def valid_coordinate?(row, column) valid_row_index?(row) && valid_column_index?(column) end |
#valid_horizontal_line_coordinates?(column, start_row, end_row) ⇒ Boolean
49 50 51 52 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 49 def valid_horizontal_line_coordinates?(column, start_row, end_row) valid_row_index?(end_row) && valid_row_index?(start_row) && valid_column_index?(column) && start_row <= end_row end |
#valid_row_index?(row) ⇒ Boolean
62 63 64 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 62 def valid_row_index?(row) row.between?(1, width) end |
#valid_vertical_line_coordinates?(row, start_column, end_column) ⇒ Boolean
44 45 46 47 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 44 def valid_vertical_line_coordinates?(row, start_column, end_column) valid_row_index?(row) && valid_column_index?(start_column) && valid_column_index?(end_column) && start_column <= end_column end |