Class: Vissen::Output::Context::Grid

Inherits:
Object
  • Object
show all
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

Attributes included from Vissen::Output::Context

#height, #palettes, #width

Instance Method Summary collapse

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.

Parameters:

  • rows (Integer)

    the number of rows in the grid.

  • columns (Integer)

    the number of columns in the grid.

  • width (Numeric) (defaults to: (columns - 1).to_f)

    (see Context)

  • height (Numeric) (defaults to: (rows - 1).to_f)

    (see Context)

Raises:

  • (RangeError)


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

#columnsInteger (readonly)

Returns the number of columns in the grid.

Returns:

  • (Integer)

    the number of columns in the grid.



20
21
22
# File 'lib/vissen/output/context/grid.rb', line 20

def columns
  @columns
end

#rowsInteger (readonly)

Returns the number of rows in the grid.

Returns:

  • (Integer)

    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_columnInteger

Iterates over each point in the grid and yields the index, row and column.

Returns:

  • (Integer)

    the number of points in the grid.



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.

Parameters:

  • row (Integer)

    the row of the point of interest.

  • column (Integer)

    the column of the point of interest.

Returns:

  • (Integer)

    the index of a row and column.



56
57
58
# File 'lib/vissen/output/context/grid.rb', line 56

def index_from(row, column)
  column * @rows + row
end

#point_countInteger

See ‘Context#point_count`.

Returns:

  • (Integer)

    the number of grid points.



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.

Parameters:

  • index (Integer)

    the index of the point of interest.

Returns:

  • (Array<Integer>)

    the row and column of a 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