Class: GridGenerator::Cubic::Grid
- Inherits:
-
Object
- Object
- GridGenerator::Cubic::Grid
- Defined in:
- lib/grid_generator/cubic/grid.rb
Constant Summary collapse
- COLOURS =
{ fill: "#d0d0d0", stroke: "#404040" }
Instance Attribute Summary collapse
-
#squares ⇒ Object
readonly
Returns the value of attribute squares.
-
#units ⇒ Object
readonly
Returns the value of attribute units.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #base_shape ⇒ Object
- #base_shape_style ⇒ Object
- #bottom_left ⇒ Object
- #bottom_right ⇒ Object
- #column_line_end(n) ⇒ Object
- #column_line_start(n) ⇒ Object
- #columns ⇒ Object
- #element_shapes ⇒ Object
- #height ⇒ Object
- #height_unit ⇒ Object
-
#initialize(x:, y:, units:, squares:) ⇒ Grid
constructor
A new instance of Grid.
- #offset_unit ⇒ Object
- #points ⇒ Object
- #row_line_end(n) ⇒ Object
- #row_line_start(n) ⇒ Object
- #rows ⇒ Object
- #square_position(row, col) ⇒ Object
- #to_svg ⇒ Object
-
#top_left ⇒ Object
positioning lines.
-
#top_left_square ⇒ Object
positioning squares.
- #top_right ⇒ Object
- #width ⇒ Object
-
#width_unit ⇒ Object
units.
Constructor Details
#initialize(x:, y:, units:, squares:) ⇒ Grid
Returns a new instance of Grid.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/grid_generator/cubic/grid.rb', line 12 def initialize(x:, y:, units: , squares: ) @x, @y = x, y @units = units @squares = case squares when String squares.split('\n').map { |r| r.split(',') } when Array squares else raise ArgumentError, "squares must be array or string" end end |
Instance Attribute Details
#squares ⇒ Object (readonly)
Returns the value of attribute squares.
25 26 27 |
# File 'lib/grid_generator/cubic/grid.rb', line 25 def squares @squares end |
#units ⇒ Object (readonly)
Returns the value of attribute units.
25 26 27 |
# File 'lib/grid_generator/cubic/grid.rb', line 25 def units @units end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
25 26 27 |
# File 'lib/grid_generator/cubic/grid.rb', line 25 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
25 26 27 |
# File 'lib/grid_generator/cubic/grid.rb', line 25 def y @y end |
Instance Method Details
#==(other) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/grid_generator/cubic/grid.rb', line 27 def ==(other) self.x == other.x && self.y == other.y && self.units == other.units && self.squares == other.squares end |
#base_shape ⇒ Object
161 162 163 |
# File 'lib/grid_generator/cubic/grid.rb', line 161 def base_shape GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style) end |
#base_shape_style ⇒ Object
157 158 159 |
# File 'lib/grid_generator/cubic/grid.rb', line 157 def base_shape_style GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke]) end |
#bottom_left ⇒ Object
79 80 81 |
# File 'lib/grid_generator/cubic/grid.rb', line 79 def bottom_left @bottom_left ||= top_left + height_unit*height end |
#bottom_right ⇒ Object
75 76 77 |
# File 'lib/grid_generator/cubic/grid.rb', line 75 def bottom_right @bottom_right ||= top_left + width_unit*width + height_unit*height end |
#column_line_end(n) ⇒ Object
95 96 97 |
# File 'lib/grid_generator/cubic/grid.rb', line 95 def column_line_end(n) bottom_left + width_unit*n end |
#column_line_start(n) ⇒ Object
91 92 93 |
# File 'lib/grid_generator/cubic/grid.rb', line 91 def column_line_start(n) top_left + width_unit*n end |
#columns ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/grid_generator/cubic/grid.rb', line 115 def columns Array.new(width) do |i| a = Matrix.column_vector([ column_line_start(i+1)[0,0], column_line_start(i+1)[1,0], ]) b = Matrix.column_vector([ column_line_end(i+1)[0,0], column_line_end(i+1)[1,0] ]) GridGenerator::BaseLine.new(a: a, b: b) end end |
#element_shapes ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/grid_generator/cubic/grid.rb', line 131 def element_shapes squares.map.each_with_index do |row, row_num| row.map.each_with_index do |col, col_num| if col GridGenerator::Cubic::SquareFactory.new( x: square_position(row_num, col_num)[0,0], y: square_position(row_num, col_num)[1,0], width_unit: width_unit, height_unit: height_unit, offset_unit: offset_unit, face: col ).build end end end.flatten.compact end |
#height ⇒ Object
38 39 40 |
# File 'lib/grid_generator/cubic/grid.rb', line 38 def height @height ||= squares.size end |
#height_unit ⇒ Object
47 48 49 |
# File 'lib/grid_generator/cubic/grid.rb', line 47 def height_unit @height_unit ||= GridGenerator::Cubic::UnitsFactory.new(side: side, type: :height, units: units).build end |
#offset_unit ⇒ Object
51 52 53 |
# File 'lib/grid_generator/cubic/grid.rb', line 51 def offset_unit @offset_unit ||= GridGenerator::Cubic::UnitsFactory.new(side: side, type: :offset, units: units).build end |
#points ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/grid_generator/cubic/grid.rb', line 148 def points [ top_left, top_right, bottom_right, bottom_left ] end |
#row_line_end(n) ⇒ Object
87 88 89 |
# File 'lib/grid_generator/cubic/grid.rb', line 87 def row_line_end(n) top_right + height_unit*n end |
#row_line_start(n) ⇒ Object
83 84 85 |
# File 'lib/grid_generator/cubic/grid.rb', line 83 def row_line_start(n) top_left + height_unit*n end |
#rows ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/grid_generator/cubic/grid.rb', line 99 def rows Array.new(height) do |i| a = Matrix.column_vector([ row_line_start(i+1)[0,0], row_line_start(i+1)[1,0], ]) b = Matrix.column_vector([ row_line_end(i+1)[0,0], row_line_end(i+1)[1,0] ]) GridGenerator::BaseLine.new(a: a, b: b) end end |
#square_position(row, col) ⇒ Object
61 62 63 |
# File 'lib/grid_generator/cubic/grid.rb', line 61 def square_position(row, col) top_left_square + width_unit*col + height_unit*row end |
#to_svg ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/grid_generator/cubic/grid.rb', line 165 def to_svg output = base_shape.to_svg rows.each { |row| output += row.to_svg } columns.each { |col| output += col.to_svg } element_shapes.each { |shape| output += shape.to_svg } output end |
#top_left ⇒ Object
positioning lines
67 68 69 |
# File 'lib/grid_generator/cubic/grid.rb', line 67 def top_left @top_left ||= Matrix.column_vector([x,y]) + offset_unit*line_offset_amount end |
#top_left_square ⇒ Object
positioning squares
57 58 59 |
# File 'lib/grid_generator/cubic/grid.rb', line 57 def top_left_square @top_left_square ||= Matrix.column_vector([x, y]) + offset_unit*square_offset_amount end |
#top_right ⇒ Object
71 72 73 |
# File 'lib/grid_generator/cubic/grid.rb', line 71 def top_right @top_right ||= top_left + width_unit*width end |
#width ⇒ Object
34 35 36 |
# File 'lib/grid_generator/cubic/grid.rb', line 34 def width @width ||= squares.first.size end |
#width_unit ⇒ Object
units
43 44 45 |
# File 'lib/grid_generator/cubic/grid.rb', line 43 def width_unit @width_unit ||= GridGenerator::Cubic::UnitsFactory.new(side: side, type: :width, units: units).build end |