Class: Sumi::Sundries::Tile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
sundries/lib/sumi/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 = Sumi::Geometry::Rect[8, 4]
tile = Sumi::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 = Sumi::Geometry::Rect[8, 4]
tile = Sumi::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 = Sumi::Sundries::Tile.new(Sumi::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:

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 (Sumi::Geometry::Element) —

    geometry element to repeat

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

    upper-left corner of the tile bounds

  • nx (Integer) (defaults to: 1) —

    number of columns

  • ny (Integer) (defaults to: 1) —

    number of rows

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'sundries/lib/sumi/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)
  @bounds = element.box

  @nx = nx
  @ny = ny
  @row_cache = {}
  @col_cache = {}
end

Instance Attribute Details

#element ⇒ Sumi::Geometry::Element (readonly)

Returns the source geometry element.



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

def element
  @element
end

#nx ⇒ Integer (readonly)

Returns the number of columns.

Returns:

  • (Integer)


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

def nx
  @nx
end

#ny ⇒ Integer (readonly)

Returns the number of rows.

Returns:

  • (Integer)


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

def ny
  @ny
end

#position ⇒ Sumi::Geometry::Point (readonly)

Returns the upper-left corner of the tile bounds.



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

def position
  @position
end

Instance Method Details

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

Returns a row by index.

Parameters:

  • i (Integer) —

    row index

Returns:

Raises:



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

def [](i) = row(i)

#at(row, col) ⇒ Sumi::Geometry::Element?

Returns the cell at the given row and column index. Negative indices count from the end. Out-of-range indices return nil.

Parameters:

  • row (Integer) —

    row index

  • col (Integer) —

    column index

Returns:

Raises:



80
81
82
83
84
85
86
# File 'sundries/lib/sumi/sundries/tile.rb', line 80

def at(row, col)
  r = index(row, ny)
  c = index(col, nx)
  return unless r && c

  @rows ? @rows[r][c] : cell_at(r, c)
end

#box ⇒ Sumi::Geometry::Rect

Returns the bounding rectangle of the whole tile.



90
# File 'sundries/lib/sumi/sundries/tile.rb', line 90

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

#cell ⇒ Sumi::Geometry::Element

Returns the first cell in the tile.



94
# File 'sundries/lib/sumi/sundries/tile.rb', line 94

def cell = @cell ||= cell_at(0, 0)

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

Returns a column by index.

Parameters:

  • i (Integer) (defaults to: 0) —

    column index

Returns:

Raises:



115
116
117
118
119
120
# File 'sundries/lib/sumi/sundries/tile.rb', line 115

def col(i = 0)
  i = index(i, nx)
  return unless i

  @cols ? @cols[i] : (@col_cache[i] ||= Array.new(ny) { |j| cell_at(j, i) }.freeze)
end

#colbox(i = 0) ⇒ Sumi::Geometry::Rect?

Returns the bounding rectangle of a column. Negative indices count from the last column. Out-of-range indices return nil.

Parameters:

  • i (Integer) (defaults to: 0) —

    column index

Returns:

Raises:



101
102
103
104
# File 'sundries/lib/sumi/sundries/tile.rb', line 101

def colbox(i = 0)
  i = index(i, nx)
  Geometry::Rect[@bounds.width, box.height, position: coordinate(0, i)] if i
end

#cols ⇒ Array<Array<Sumi::Geometry::Element>>

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

Returns:



109
# File 'sundries/lib/sumi/sundries/tile.rb', line 109

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

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

Iterates over rows.

Yields:

  • (row) —

    each row

Yield Parameters:

Yield Returns:

  • (void)

Returns:



127
# File 'sundries/lib/sumi/sundries/tile.rb', line 127

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

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

Iterates over columns.

Yields:

  • (column) —

    each column

Yield Parameters:

Yield Returns:

  • (void)

Returns:



134
# File 'sundries/lib/sumi/sundries/tile.rb', line 134

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

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

Returns a row by index.

Parameters:

  • i (Integer) (defaults to: 0) —

    row index

Returns:

Raises:



140
141
142
143
144
145
# File 'sundries/lib/sumi/sundries/tile.rb', line 140

def row(i = 0)
  i = index(i, ny)
  return unless i

  @rows ? @rows[i] : (@row_cache[i] ||= Array.new(nx) { |j| cell_at(i, j) }.freeze)
end

#rowbox(i = 0) ⇒ Sumi::Geometry::Rect?

Returns the bounding rectangle of a row. Negative indices count from the last row. Out-of-range indices return nil.

Parameters:

  • i (Integer) (defaults to: 0) —

    row index

Returns:

Raises:



152
153
154
155
# File 'sundries/lib/sumi/sundries/tile.rb', line 152

def rowbox(i = 0)
  i = index(i, ny)
  Geometry::Rect[box.width, @bounds.height, position: coordinate(i)] if i
end

#rows ⇒ Array<Array<Sumi::Geometry::Element>>

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

Returns:



160
# File 'sundries/lib/sumi/sundries/tile.rb', line 160

def rows = @rows ||= Array.new(ny) { |i| @row_cache[i] || Array.new(nx) { |j| cell_at(i, j) }.freeze }.freeze