Class: Sumi::Sundries::Tile
- Inherits:
-
Object
- Object
- Sumi::Sundries::Tile
- 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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#element ⇒ Sumi::Geometry::Element
readonly
Returns the source geometry element.
-
#nx ⇒ Integer
readonly
Returns the number of columns.
-
#ny ⇒ Integer
readonly
Returns the number of rows.
-
#position ⇒ Sumi::Geometry::Point
readonly
Returns the upper-left corner of the tile bounds.
Instance Method Summary collapse
-
#[](i) ⇒ Array<Sumi::Geometry::Element>?
Returns a row by index.
-
#at(row, col) ⇒ Sumi::Geometry::Element?
Returns the cell at the given row and column index.
-
#box ⇒ Sumi::Geometry::Rect
Returns the bounding rectangle of the whole tile.
-
#cell ⇒ Sumi::Geometry::Element
Returns the first cell in the tile.
-
#col(i = 0) ⇒ Array<Sumi::Geometry::Element>?
Returns a column by index.
-
#colbox(i = 0) ⇒ Sumi::Geometry::Rect?
Returns the bounding rectangle of a column.
-
#cols ⇒ Array<Array<Sumi::Geometry::Element>>
Returns cells grouped by column.
-
#each {|row| ... } ⇒ Enumerator, Array<Array<Sumi::Geometry::Element>>
(also: #each_row)
Iterates over rows.
-
#each_col {|column| ... } ⇒ Enumerator, Array<Array<Sumi::Geometry::Element>>
Iterates over columns.
-
#initialize(element, position: Geometry::Origin, nx: 1, ny: 1) ⇒ void
constructor
Creates a tile from a source geometry element.
-
#row(i = 0) ⇒ Array<Sumi::Geometry::Element>?
Returns a row by index.
-
#rowbox(i = 0) ⇒ Sumi::Geometry::Rect?
Returns the bounding rectangle of a row.
-
#rows ⇒ Array<Array<Sumi::Geometry::Element>>
Returns cells grouped by row.
Constructor Details
#initialize(element, position: Geometry::Origin, nx: 1, ny: 1) ⇒ void
Creates a tile from a source geometry element.
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.
38 39 40 |
# File 'sundries/lib/sumi/sundries/tile.rb', line 38 def nx @nx end |
#ny ⇒ Integer (readonly)
Returns the number of rows.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |