Class: GridGenerator::Cubic::FacingGrid

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/cubic/facing_grid.rb

Constant Summary collapse

COLOURS =
{
  fill: "#d0d0d0",
  stroke: "#404040"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, units:, squares:) ⇒ FacingGrid

Returns a new instance of FacingGrid.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/grid_generator/cubic/facing_grid.rb', line 16

def initialize(x:, y:, units:, squares: )
  @x, @y = x, y
  @units = units
  @squares = case squares
  when String
    FaceParser.new(squares).parse
  when Array
    squares
  else
    raise ArgumentError, "squares must be array or string" 
  end
end

Instance Attribute Details

#squaresObject (readonly)

Returns the value of attribute squares.



29
30
31
# File 'lib/grid_generator/cubic/facing_grid.rb', line 29

def squares
  @squares
end

#unitsObject (readonly)

Returns the value of attribute units.



29
30
31
# File 'lib/grid_generator/cubic/facing_grid.rb', line 29

def units
  @units
end

#xObject (readonly)

Returns the value of attribute x.



29
30
31
# File 'lib/grid_generator/cubic/facing_grid.rb', line 29

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



29
30
31
# File 'lib/grid_generator/cubic/facing_grid.rb', line 29

def y
  @y
end

Instance Method Details

#base_shapeObject



118
119
120
# File 'lib/grid_generator/cubic/facing_grid.rb', line 118

def base_shape
  GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
end

#base_shape_styleObject



114
115
116
# File 'lib/grid_generator/cubic/facing_grid.rb', line 114

def base_shape_style
  GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
end

#columnsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/grid_generator/cubic/facing_grid.rb', line 79

def columns 
  Array.new(width) do |i|
    a = Matrix.column_vector([
      unit_x(i+1),
      y,
    ])

    b = Matrix.column_vector([
      unit_x(i+1),
      max_y
    ])

    GridGenerator::BaseLine.new(a: a, b: b)
  end
end

#element_shapesObject



95
96
97
98
99
100
101
102
103
# File 'lib/grid_generator/cubic/facing_grid.rb', line 95

def element_shapes
  squares.map.each_with_index do |row, row_num|
    row.map.each_with_index do |col, col_num|
      if col 
        Cubic::FacingSquareFactory.new(x: unit_x(col_num), y: unit_y(row_num), width: unit_width(col_num), height: unit_height(row_num), colour: col[:colour], opacity: col[:opacity]).build
      end
    end
  end.flatten.compact
end

#heightObject



35
36
37
# File 'lib/grid_generator/cubic/facing_grid.rb', line 35

def height
  squares.size
end

#max_xObject



39
40
41
# File 'lib/grid_generator/cubic/facing_grid.rb', line 39

def max_x 
  x + width * units
end

#max_yObject



43
44
45
# File 'lib/grid_generator/cubic/facing_grid.rb', line 43

def max_y
  y + height * units
end

#pointsObject



105
106
107
108
109
110
111
112
# File 'lib/grid_generator/cubic/facing_grid.rb', line 105

def points
  [
    Matrix.column_vector([ x, y ]),
    Matrix.column_vector([ max_x, y ]),
    Matrix.column_vector([ max_x, max_y ]),
    Matrix.column_vector([ x, max_y ])
  ]
end

#rowsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/grid_generator/cubic/facing_grid.rb', line 63

def rows
  Array.new(height) do |i|
    a = Matrix.column_vector([
      x,
      unit_y(i+1),
    ])

    b = Matrix.column_vector([
      max_x,
      unit_y(i+1)
    ])

    GridGenerator::BaseLine.new(a: a, b: b) 
  end
end

#to_svgObject



122
123
124
125
126
127
128
129
130
# File 'lib/grid_generator/cubic/facing_grid.rb', line 122

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

#unit_height(_) ⇒ Object



59
60
61
# File 'lib/grid_generator/cubic/facing_grid.rb', line 59

def unit_height(_)
  units
end

#unit_width(_) ⇒ Object



55
56
57
# File 'lib/grid_generator/cubic/facing_grid.rb', line 55

def unit_width(_)
  units
end

#unit_x(n) ⇒ Object



47
48
49
# File 'lib/grid_generator/cubic/facing_grid.rb', line 47

def unit_x(n)
  x + n * units
end

#unit_y(n) ⇒ Object



51
52
53
# File 'lib/grid_generator/cubic/facing_grid.rb', line 51

def unit_y(n)
  y + n * units
end

#widthObject



31
32
33
# File 'lib/grid_generator/cubic/facing_grid.rb', line 31

def width
  squares.first.size 
end