Class: Sevgi::Sundries::Tile
- Inherits:
-
Object
- Object
- Sevgi::Sundries::Tile
- Includes:
- Enumerable
- Defined in:
- lib/sevgi/sundries/tile.rb
Overview
Repeats a geometry element over a rectangular row and column layout.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#element ⇒ Sevgi::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 ⇒ Sevgi::Geometry::Point
readonly
Returns the tile origin.
Instance Method Summary collapse
-
#[](i) ⇒ Array<Sevgi::Geometry::Element>?
Returns a row by index.
-
#box ⇒ Sevgi::Geometry::Rect
Returns the bounding rectangle of the whole tile.
-
#cell ⇒ Sevgi::Geometry::Element
Returns the first cell in the tile.
-
#col(i = 0) ⇒ Array<Sevgi::Geometry::Element>?
Returns a column by index.
-
#colbox(i = 0) ⇒ Sevgi::Geometry::Rect
Returns the bounding rectangle of a column.
-
#cols ⇒ Array<Array<Sevgi::Geometry::Element>>
Returns cells grouped by column.
-
#each {|row| ... } ⇒ Enumerator, Array<Array<Sevgi::Geometry::Element>>
(also: #each_row)
Iterates over rows.
-
#each_col {|column| ... } ⇒ Enumerator, Array<Array<Sevgi::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<Sevgi::Geometry::Element>?
Returns a row by index.
-
#rowbox(i = 0) ⇒ Sevgi::Geometry::Rect
Returns the bounding rectangle of a row.
-
#rows ⇒ Array<Array<Sevgi::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.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sevgi/sundries/tile.rb', line 34 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
#element ⇒ Sevgi::Geometry::Element (readonly)
Returns the source geometry element.
11 12 13 |
# File 'lib/sevgi/sundries/tile.rb', line 11 def element @element end |
#nx ⇒ Integer (readonly)
Returns the number of columns.
19 20 21 |
# File 'lib/sevgi/sundries/tile.rb', line 19 def nx @nx end |
#ny ⇒ Integer (readonly)
Returns the number of rows.
23 24 25 |
# File 'lib/sevgi/sundries/tile.rb', line 23 def ny @ny end |
#position ⇒ Sevgi::Geometry::Point (readonly)
Returns the tile origin.
15 16 17 |
# File 'lib/sevgi/sundries/tile.rb', line 15 def position @position end |
Instance Method Details
#[](i) ⇒ Array<Sevgi::Geometry::Element>?
Returns a row by index.
49 |
# File 'lib/sevgi/sundries/tile.rb', line 49 def [](i) = rows[i] |
#box ⇒ Sevgi::Geometry::Rect
Returns the bounding rectangle of the whole tile.
53 |
# File 'lib/sevgi/sundries/tile.rb', line 53 def box = @box ||= Geometry::Rect[nx * element.box.width, ny * element.box.height, position:] |
#cell ⇒ Sevgi::Geometry::Element
Returns the first cell in the tile.
57 |
# File 'lib/sevgi/sundries/tile.rb', line 57 def cell = row.first |
#col(i = 0) ⇒ Array<Sevgi::Geometry::Element>?
Returns a column by index.
72 |
# File 'lib/sevgi/sundries/tile.rb', line 72 def col(i = 0) = cols[i] |
#colbox(i = 0) ⇒ Sevgi::Geometry::Rect
Returns the bounding rectangle of a column.
62 |
# File 'lib/sevgi/sundries/tile.rb', line 62 def colbox(i = 0) = Geometry::Rect[element.box.width, box.height, position: coordinate(0, i)] |
#cols ⇒ Array<Array<Sevgi::Geometry::Element>>
Returns cells grouped by column. The outer and nested collections are frozen and must be treated as immutable.
67 |
# File 'lib/sevgi/sundries/tile.rb', line 67 def cols = @cols ||= rows.transpose.map(&:freeze).freeze |
#each {|row| ... } ⇒ Enumerator, Array<Array<Sevgi::Geometry::Element>> Also known as: each_row
Iterates over rows.
79 |
# File 'lib/sevgi/sundries/tile.rb', line 79 def each(...) = rows.each(...) |
#each_col {|column| ... } ⇒ Enumerator, Array<Array<Sevgi::Geometry::Element>>
Iterates over columns.
86 |
# File 'lib/sevgi/sundries/tile.rb', line 86 def each_col(...) = cols.each(...) |
#row(i = 0) ⇒ Array<Sevgi::Geometry::Element>?
Returns a row by index.
91 |
# File 'lib/sevgi/sundries/tile.rb', line 91 def row(i = 0) = rows[i] |
#rowbox(i = 0) ⇒ Sevgi::Geometry::Rect
Returns the bounding rectangle of a row.
96 |
# File 'lib/sevgi/sundries/tile.rb', line 96 def rowbox(i = 0) = Geometry::Rect[box.width, element.box.height, position: coordinate(i)] |
#rows ⇒ Array<Array<Sevgi::Geometry::Element>>
Returns cells grouped by row. The outer and nested collections are frozen and must be treated as immutable.
101 |
# File 'lib/sevgi/sundries/tile.rb', line 101 def rows = @rows ||= (0...ny).map { |i| (0...nx).map { |j| element.at(coordinate(i, j)) }.freeze }.freeze |