Class: BitmapCompiler::Core::Bitmap
- Inherits:
-
Object
- Object
- BitmapCompiler::Core::Bitmap
- Defined in:
- lib/bitmap_compiler/core/bitmap.rb
Overview
Bitmap entity
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.
11 12 13 14 15 16 17 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 11 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.
9 10 11 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 9 def cells @cells end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
9 10 11 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 9 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
9 10 11 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 9 def width @width end |
Instance Method Details
#change_pixel(row, column, color) ⇒ Object
19 20 21 22 23 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 19 def change_pixel(row, column, color) return false unless valid_coordinate?(row, column) cells[row - 1][column - 1] = color end |
#clear ⇒ Object
25 26 27 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 25 def clear @cells = initialize_matrix end |
#initialize_matrix ⇒ Object
29 30 31 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 29 def initialize_matrix Array.new(width) { Array.new(height) { STANDARD_COLOR } } end |
#print ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 33 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
59 60 61 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 59 def valid_column_index?(column) column.between?(1, height) end |
#valid_coordinate?(row, column) ⇒ Boolean
55 56 57 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 55 def valid_coordinate?(row, column) valid_row_index?(row) && valid_column_index?(column) end |
#valid_horizontal_line_coordinates?(column, start_row, end_row) ⇒ Boolean
50 51 52 53 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 50 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
63 64 65 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 63 def valid_row_index?(row) row.between?(1, width) end |
#valid_vertical_line_coordinates?(row, start_column, end_column) ⇒ Boolean
45 46 47 48 |
# File 'lib/bitmap_compiler/core/bitmap.rb', line 45 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 |