Class: Sevgi::Sundries::Tile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sevgi/sundries/tile.rb

Overview

Repeats a geometry element over a rectangular row and column layout.

The source element's bounding-box width and height become the cell pitch. Indexing is row-first: tile[row][column]. Generated cells and collection snapshots are immutable geometry values; no SVG elements are created.

Examples:

Address cells by row and column

cell = Sevgi::Geometry::Rect[8, 4]
tile = Sevgi::Sundries::Tile.new(cell, position: [10, 20], nx: 3, ny: 2)
tile[1][2].position.deconstruct # => [26.0, 24.0]

Inspect the complete, row, and column bounds

cell = Sevgi::Geometry::Rect[8, 4]
tile = Sevgi::Sundries::Tile.new(cell, position: [10, 20], nx: 3, ny: 2)
[tile.box.approx.width, tile.box.approx.height] # => [24.0, 8.0]
tile.rowbox(1).position.deconstruct # => [10.0, 24.0]
tile.colbox(2).position.deconstruct # => [26.0, 20.0]

Iterate by rows or columns

tile = Sevgi::Sundries::Tile.new(Sevgi::Geometry::Rect[8, 4], nx: 3, ny: 2)
tile.each_row.map(&:size) # => [3, 3]
tile.each_col.map(&:size) # => [2, 2, 2]

See Also:

  • Graphics::Mixtures::Tile

Direct Known Subclasses

Grid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, position: Geometry::Origin, nx: 1, ny: 1) ⇒ void

Creates a tile from a source geometry element.

Parameters:

  • element (Sevgi::Geometry::Element)

    geometry element to repeat

  • position (Sevgi::Geometry::Point, Array<Numeric>) (defaults to: Geometry::Origin)

    tile origin

  • nx (Integer) (defaults to: 1)

    number of columns

  • ny (Integer) (defaults to: 1)

    number of rows

Raises:

  • (Sevgi::ArgumentError)

    when element is not a geometry element

  • (Sevgi::ArgumentError)

    when nx or ny is not a positive integer

  • (Sevgi::ArgumentError)

    when position is not a point or two-number array



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sevgi/sundries/tile.rb', line 53

def initialize(element, position: Geometry::Origin, nx: 1, ny: 1)
  ArgumentError.("Must be an Element object: #{element}") unless element.is_a?(Geometry::Element)
  ArgumentError.("Tile nx must be positive") unless nx.is_a?(::Integer) && nx.positive?
  ArgumentError.("Tile ny must be positive") unless ny.is_a?(::Integer) && ny.positive?

  @element = element
  @position = self.class.send(:position, position)

  @nx = nx
  @ny = ny
end

Instance Attribute Details

#elementSevgi::Geometry::Element (readonly)

Returns the source geometry element.

Returns:

  • (Sevgi::Geometry::Element)


30
31
32
# File 'lib/sevgi/sundries/tile.rb', line 30

def element
  @element
end

#nxInteger (readonly)

Returns the number of columns.

Returns:

  • (Integer)


38
39
40
# File 'lib/sevgi/sundries/tile.rb', line 38

def nx
  @nx
end

#nyInteger (readonly)

Returns the number of rows.

Returns:

  • (Integer)


42
43
44
# File 'lib/sevgi/sundries/tile.rb', line 42

def ny
  @ny
end

#positionSevgi::Geometry::Point (readonly)

Returns the tile origin.

Returns:

  • (Sevgi::Geometry::Point)


34
35
36
# File 'lib/sevgi/sundries/tile.rb', line 34

def position
  @position
end

Instance Method Details

#[](i) ⇒ Array<Sevgi::Geometry::Element>?

Returns a row by index.

Parameters:

  • i (Integer)

    row index

Returns:

  • (Array<Sevgi::Geometry::Element>, nil)


68
# File 'lib/sevgi/sundries/tile.rb', line 68

def [](i) = rows[i]

#boxSevgi::Geometry::Rect

Returns the bounding rectangle of the whole tile.

Returns:

  • (Sevgi::Geometry::Rect)


72
# File 'lib/sevgi/sundries/tile.rb', line 72

def box = @box ||= Geometry::Rect[nx * element.box.width, ny * element.box.height, position:]

#cellSevgi::Geometry::Element

Returns the first cell in the tile.

Returns:

  • (Sevgi::Geometry::Element)


76
# File 'lib/sevgi/sundries/tile.rb', line 76

def cell = row.first

#col(i = 0) ⇒ Array<Sevgi::Geometry::Element>?

Returns a column by index.

Parameters:

  • i (Integer) (defaults to: 0)

    column index

Returns:

  • (Array<Sevgi::Geometry::Element>, nil)


91
# File 'lib/sevgi/sundries/tile.rb', line 91

def col(i = 0) = cols[i]

#colbox(i = 0) ⇒ Sevgi::Geometry::Rect

Returns the bounding rectangle of a column.

Parameters:

  • i (Integer) (defaults to: 0)

    column index

Returns:

  • (Sevgi::Geometry::Rect)


81
# File 'lib/sevgi/sundries/tile.rb', line 81

def colbox(i = 0) = Geometry::Rect[element.box.width, box.height, position: coordinate(0, i)]

#colsArray<Array<Sevgi::Geometry::Element>>

Returns cells grouped by column. The outer and nested collections are frozen and must be treated as immutable.

Returns:

  • (Array<Array<Sevgi::Geometry::Element>>)

    frozen columns



86
# File 'lib/sevgi/sundries/tile.rb', line 86

def cols = @cols ||= rows.transpose.map(&:freeze).freeze

#each {|row| ... } ⇒ Enumerator, Array<Array<Sevgi::Geometry::Element>> Also known as: each_row

Iterates over rows.

Yields:

  • (row)

    each row

Yield Parameters:

  • row (Array<Sevgi::Geometry::Element>)

    row cells

Yield Returns:

  • (void)

Returns:

  • (Enumerator, Array<Array<Sevgi::Geometry::Element>>)

    enumerator without a block, otherwise rows



98
# File 'lib/sevgi/sundries/tile.rb', line 98

def each(...) = rows.each(...)

#each_col {|column| ... } ⇒ Enumerator, Array<Array<Sevgi::Geometry::Element>>

Iterates over columns.

Yields:

  • (column)

    each column

Yield Parameters:

  • column (Array<Sevgi::Geometry::Element>)

    column cells

Yield Returns:

  • (void)

Returns:

  • (Enumerator, Array<Array<Sevgi::Geometry::Element>>)

    enumerator without a block, otherwise columns



105
# File 'lib/sevgi/sundries/tile.rb', line 105

def each_col(...) = cols.each(...)

#row(i = 0) ⇒ Array<Sevgi::Geometry::Element>?

Returns a row by index.

Parameters:

  • i (Integer) (defaults to: 0)

    row index

Returns:

  • (Array<Sevgi::Geometry::Element>, nil)


110
# File 'lib/sevgi/sundries/tile.rb', line 110

def row(i = 0) = rows[i]

#rowbox(i = 0) ⇒ Sevgi::Geometry::Rect

Returns the bounding rectangle of a row.

Parameters:

  • i (Integer) (defaults to: 0)

    row index

Returns:

  • (Sevgi::Geometry::Rect)


115
# File 'lib/sevgi/sundries/tile.rb', line 115

def rowbox(i = 0) = Geometry::Rect[box.width, element.box.height, position: coordinate(i)]

#rowsArray<Array<Sevgi::Geometry::Element>>

Returns cells grouped by row. The outer and nested collections are frozen and must be treated as immutable.

Returns:

  • (Array<Array<Sevgi::Geometry::Element>>)

    frozen rows



120
# File 'lib/sevgi/sundries/tile.rb', line 120

def rows = @rows ||= (0...ny).map { |i| (0...nx).map { |j| element.at(coordinate(i, j)) }.freeze }.freeze