Class: GridGenerator::Cubic::BorderedGrid

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/cubic/bordered_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:) ⇒ BorderedGrid

Returns a new instance of BorderedGrid.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/grid_generator/cubic/bordered_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/bordered_grid.rb', line 29

def squares
  @squares
end

#unitsObject (readonly)

Returns the value of attribute units.



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

def units
  @units
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#base_shapeObject



138
139
140
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 138

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

#base_shape_styleObject



134
135
136
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 134

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

#border_unitObject



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

def border_unit
  units / 4
end

#columnsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 99

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



115
116
117
118
119
120
121
122
123
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 115

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/bordered_grid.rb', line 35

def height
  squares.size
end

#max_xObject



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

def max_x 
  x + (width - 2) * units + (2 * border_unit)
end

#max_yObject



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

def max_y
  y + (height - 2) * units + (2 * border_unit)
end

#pointsObject



125
126
127
128
129
130
131
132
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 125

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 83

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



142
143
144
145
146
147
148
149
150
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 142

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(n) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 75

def unit_height(n)
  if n == 0 || n == (height - 1)
    border_unit
  else
    units
  end
end

#unit_width(n) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 67

def unit_width(n)
  if n == 0 || n == (width - 1)
    border_unit
  else
    units
  end
end

#unit_x(n) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 51

def unit_x(n)
  if n == 0
    x
  else
    x + border_unit + (n - 1) * units 
  end
end

#unit_y(n) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/grid_generator/cubic/bordered_grid.rb', line 59

def unit_y(n)
  if n == 0
    y
  else
    y + border_unit + (n - 1) * units
  end
end

#widthObject



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

def width
  squares.first.size 
end