Class: Vissen::Output::Context::Grid
- Inherits:
-
Object
- Object
- Vissen::Output::Context::Grid
- Includes:
- Vissen::Output::Context
- Defined in:
- lib/vissen/output/context/grid.rb
Overview
The grid structure stores the number of rows and columns as well as its width and height. The dimensions are normalized to fit within a 1x1 square.
Aspect ratio is defined as width/height. If it is not given each grid cell is assumed to be square, meaning the aspect_ratio will equal columns/rows.
Instance Attribute Summary collapse
-
#columns ⇒ Integer
readonly
The number of columns in the grid.
-
#rows ⇒ Integer
readonly
The number of rows in the grid.
Attributes included from Vissen::Output::Context
Instance Method Summary collapse
-
#each_row_and_column ⇒ Integer
Iterates over each point in the grid and yields the index, row and column.
-
#index_from(row, column) ⇒ Integer
See ‘Context#index_from`.
-
#initialize(rows, columns, width: (columns - 1).to_f, height: (rows - 1).to_f, **args) ⇒ Grid
constructor
A new instance of Grid.
-
#point_count ⇒ Integer
See ‘Context#point_count`.
-
#row_column_from(index) ⇒ Array<Integer>
Calculates the row and column of the the point stored at the given index.
Methods included from Vissen::Output::Context
#alloc_points, #center, #each, #each_distance_squared, #each_polar_offset, #each_position, #one_dimensional?, #position
Constructor Details
#initialize(rows, columns, width: (columns - 1).to_f, height: (rows - 1).to_f, **args) ⇒ Grid
Returns a new instance of Grid.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vissen/output/context/grid.rb', line 27 def initialize(rows, columns, width: (columns - 1).to_f, height: (rows - 1).to_f, **args) raise RangeError if rows <= 0 || columns <= 0 @rows = rows @columns = columns super(width, height, **args) define_position end |
Instance Attribute Details
#columns ⇒ Integer (readonly)
Returns the number of columns in the grid.
20 21 22 |
# File 'lib/vissen/output/context/grid.rb', line 20 def columns @columns end |
#rows ⇒ Integer (readonly)
Returns the number of rows in the grid.
17 18 19 |
# File 'lib/vissen/output/context/grid.rb', line 17 def rows @rows end |
Instance Method Details
#each_row_and_column ⇒ Integer
Iterates over each point in the grid and yields the index, row and column.
75 76 77 78 79 |
# File 'lib/vissen/output/context/grid.rb', line 75 def each_row_and_column return to_enum(__callee__) unless block_given? point_count.times { |i| yield(i, *row_column_from(i)) } end |
#index_from(row, column) ⇒ Integer
See ‘Context#index_from`.
WARNING: no range check is performed.
56 57 58 |
# File 'lib/vissen/output/context/grid.rb', line 56 def index_from(row, column) column * @rows + row end |
#point_count ⇒ Integer
See ‘Context#point_count`.
45 46 47 |
# File 'lib/vissen/output/context/grid.rb', line 45 def point_count @rows * @columns end |
#row_column_from(index) ⇒ Array<Integer>
Calculates the row and column of the the point stored at the given index.
65 66 67 68 69 |
# File 'lib/vissen/output/context/grid.rb', line 65 def row_column_from(index) row = (index % @rows) column = (index / @rows) [row, column] end |