Class: GridGenerator::Megaminx::FaceProjection

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/megaminx/face_projection.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, units:, front_face_elements: "", top_right_face_elements: "", right_face_elements: "", down_face_elements: "", left_face_elements: "", top_left_face_elements: "", rotation_offset: 0) ⇒ FaceProjection

units 30 - pentagon 90 - megaminx - 150 units * 5



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/grid_generator/megaminx/face_projection.rb', line 18

def initialize(x:, y:, units:, front_face_elements: "", top_right_face_elements: "", right_face_elements: "", down_face_elements: "", left_face_elements: "", top_left_face_elements: "", rotation_offset: 0)
  @x, @y = x, y
  @units = units
  @front_face_elements = front_face_elements.split(',')
  @top_right_face_elements = top_right_face_elements.split(',')
  @right_face_elements = right_face_elements.split(',')
  @down_face_elements = down_face_elements.split(',')
  @left_face_elements = left_face_elements.split(',')
  @top_left_face_elements = top_left_face_elements.split(',')
  @rotation_offset = rotation_offset
end

Instance Attribute Details

#down_face_elementsObject (readonly)

Returns the value of attribute down_face_elements.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def down_face_elements
  @down_face_elements
end

#front_face_elementsObject (readonly)

Returns the value of attribute front_face_elements.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def front_face_elements
  @front_face_elements
end

#left_face_elementsObject (readonly)

Returns the value of attribute left_face_elements.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def left_face_elements
  @left_face_elements
end

#right_face_elementsObject (readonly)

Returns the value of attribute right_face_elements.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def right_face_elements
  @right_face_elements
end

#rotation_offsetObject (readonly)

Returns the value of attribute rotation_offset.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def rotation_offset
  @rotation_offset
end

#top_left_face_elementsObject (readonly)

Returns the value of attribute top_left_face_elements.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def top_left_face_elements
  @top_left_face_elements
end

#top_right_face_elementsObject (readonly)

Returns the value of attribute top_right_face_elements.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def top_right_face_elements
  @top_right_face_elements
end

#unitsObject (readonly)

Returns the value of attribute units.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def units
  @units
end

#xObject (readonly)

Returns the value of attribute x.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



30
31
32
# File 'lib/grid_generator/megaminx/face_projection.rb', line 30

def y
  @y
end

Instance Method Details

#connecting_linesObject

for svg



164
165
166
167
168
169
# File 'lib/grid_generator/megaminx/face_projection.rb', line 164

def connecting_lines
  pentagon_points.each_with_index.map do |p, i|
    d = decagon_points[i*2]
    offset_rotator.rotate(GridGenerator::BaseLine.new(a: p, b: d)) + offset
  end
end

#decagon_pointsObject



68
69
70
71
72
73
74
# File 'lib/grid_generator/megaminx/face_projection.rb', line 68

def decagon_points
  @decagon_points ||= (0..9).map do |i|
    angle = 2.0 * Math::PI * i / 10.0
    rotator = GridGenerator::Rotator.new(angle: angle, rotation_point: rotation_point)    
    rotator.rotate(decagon_top_point)
  end
end

#decagon_points_transformedObject

for svg



154
155
156
# File 'lib/grid_generator/megaminx/face_projection.rb', line 154

def decagon_points_transformed
  decagon_points.map { |p| offset_rotator.rotate(p) + offset }
end

#decagon_radiusObject



50
51
52
# File 'lib/grid_generator/megaminx/face_projection.rb', line 50

def decagon_radius
  5 * units
end

#decagon_shapeObject



219
220
221
# File 'lib/grid_generator/megaminx/face_projection.rb', line 219

def decagon_shape
  GridGenerator::Svg::Polygon.new(points: decagon_points_transformed, style: decagon_shape_style)
end

#decagon_shape_styleObject



215
216
217
# File 'lib/grid_generator/megaminx/face_projection.rb', line 215

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

#decagon_side_lengthObject

also equal to pentagon radius



77
78
79
# File 'lib/grid_generator/megaminx/face_projection.rb', line 77

def decagon_side_length 
  @decagon_side_length ||= GridGenerator::Helper.distance(decagon_points[0], decagon_points[1])
end

#decagon_top_pointObject



54
55
56
57
58
59
# File 'lib/grid_generator/megaminx/face_projection.rb', line 54

def decagon_top_point
  Matrix.column_vector([
    decagon_radius,
    0 
  ])
end

#front_face_element_shapesObject

for svg



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/grid_generator/megaminx/face_projection.rb', line 184

def front_face_element_shapes
  front_face_elements.each_with_index.map do |element, i|
    GridGenerator::Megaminx::FaceElementFactory.new(
      x: x,
      y: y,
      index: i,
      face_points: pentagon_points,
      face_lines: front_face_lines_raw,
      face: element,
      rotator: offset_rotator
    ).build unless element == '-'
  end.compact
end

#front_face_linesObject

for svg



172
173
174
# File 'lib/grid_generator/megaminx/face_projection.rb', line 172

def front_face_lines
  front_face_lines_raw.map { |l| offset_rotator.rotate(l) + offset }
end

#front_face_lines_rawObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/grid_generator/megaminx/face_projection.rb', line 113

def front_face_lines_raw
  (0..4).map do |i|
    a = pentagon_points[i-1] # offset one so first line is top right
    b = pentagon_points[(i)%5]
    c = pentagon_points[(i+1)%5]
    d = pentagon_points[(i+2)%5]

    ab_intervals = GridGenerator::Helper.intervals(a,b,2)
    cd_intervals = GridGenerator::Helper.intervals(c,d,2)

    GridGenerator::BaseLine.new(a: ab_intervals[-1], b: cd_intervals[0])
  end
end

#offsetObject



42
43
44
# File 'lib/grid_generator/megaminx/face_projection.rb', line 42

def offset
  @offset ||= Matrix.column_vector([x, y])
end

#offset_rotatorObject



46
47
48
# File 'lib/grid_generator/megaminx/face_projection.rb', line 46

def offset_rotator
  @offset_rotator ||= GridGenerator::Rotator.new(angle: rotation_offset, rotation_point: rotation_point)    
end

#outside_face_element_shapesObject

for svg



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/grid_generator/megaminx/face_projection.rb', line 199

def outside_face_element_shapes
  outside_face_elements.each_with_index.map do |face_elements, face_index|
    face_elements.each_with_index.map do |element, element_index|
      GridGenerator::Megaminx::FaceElementFactory.new(
        x: x,
        y: y,
        index: element_index,
        face_points: outside_pentagon_points[face_index],
        face_lines: outside_face_lines_raw[face_index],
        face: element,
        rotator: offset_rotator
      ).build unless element == '-'
    end.compact
  end
end

#outside_face_elementsObject



32
33
34
35
36
37
38
39
40
# File 'lib/grid_generator/megaminx/face_projection.rb', line 32

def outside_face_elements
  [
    top_right_face_elements,
    right_face_elements,
    down_face_elements,
    left_face_elements,
    top_left_face_elements
  ]
end

#outside_face_linesObject

for svg



177
178
179
180
181
# File 'lib/grid_generator/megaminx/face_projection.rb', line 177

def outside_face_lines
  outside_face_lines_raw.map do |face_lines|
    face_lines.map { |l| offset_rotator.rotate(l) + offset }
  end
end

#outside_face_lines_rawObject



141
142
143
144
145
146
# File 'lib/grid_generator/megaminx/face_projection.rb', line 141

def outside_face_lines_raw
  @outside_face_lines_raw ||= (0..4).map do |i|
    rotator = GridGenerator::Rotator.new(angle: Math::PI * i * 0.4, rotation_point: rotation_point)
    top_right_face_lines_raw.map { |l| rotator.rotate(l) } 
  end
end

#outside_pentagon_pointsObject



106
107
108
109
110
111
# File 'lib/grid_generator/megaminx/face_projection.rb', line 106

def outside_pentagon_points
  @outside_pentagon_points ||= (0..4).map do |i|
    rotator = GridGenerator::Rotator.new(angle: Math::PI * i * 0.4, rotation_point: rotation_point)
    top_right_pentagon_points.map { |p| rotator.rotate(p) } 
  end
end

#pentagon_pointsObject



88
89
90
91
92
93
94
# File 'lib/grid_generator/megaminx/face_projection.rb', line 88

def pentagon_points 
  @pentagon_points ||= (0..4).map do |i|
    angle = 2.0 * Math::PI * i / 5.0
    rotator = GridGenerator::Rotator.new(angle: angle, rotation_point: rotation_point)    
    rotator.rotate(pentagon_top_point)
  end
end

#pentagon_points_transformedObject

for svg



159
160
161
# File 'lib/grid_generator/megaminx/face_projection.rb', line 159

def pentagon_points_transformed
  pentagon_points.map { |p| offset_rotator.rotate(p) + offset }
end

#pentagon_shapeObject



227
228
229
# File 'lib/grid_generator/megaminx/face_projection.rb', line 227

def pentagon_shape
  GridGenerator::Svg::Polygon.new(points: pentagon_points_transformed, style: pentagon_shape_style)
end

#pentagon_shape_styleObject



223
224
225
# File 'lib/grid_generator/megaminx/face_projection.rb', line 223

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

#pentagon_top_pointObject



81
82
83
84
85
86
# File 'lib/grid_generator/megaminx/face_projection.rb', line 81

def pentagon_top_point
  @pentagon_top_point ||= Matrix.column_vector([
    decagon_radius,
    decagon_radius - decagon_side_length 
  ])
end

#rotation_pointObject



61
62
63
64
65
66
# File 'lib/grid_generator/megaminx/face_projection.rb', line 61

def rotation_point
  Matrix.column_vector([
    decagon_radius,
    decagon_radius
  ])
end

#to_svgObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/grid_generator/megaminx/face_projection.rb', line 231

def to_svg
  output = decagon_shape.to_svg
  output += pentagon_shape.to_svg

  connecting_lines.each { |line| output += line.to_svg }

  front_face_lines.each { |line| output += line.to_svg }
  outside_face_lines.each do |face|
    face.each { |line| output += line.to_svg }
  end

  front_face_element_shapes.each { |shape| output += shape.to_svg }
  outside_face_element_shapes.each do |face|
    face.each { |shape| output += shape.to_svg }
  end

  output
end

#top_left_face_lines_rawObject



148
149
150
151
# File 'lib/grid_generator/megaminx/face_projection.rb', line 148

def top_left_face_lines_raw
  rotator = GridGenerator::Rotator.new(angle: Math::PI * 1.6, rotation_point: rotation_point)
  @top_left_face_lines_raw ||= outside_face_lines_raw[0].map { |l| rotator.rotate(l) } 
end

#top_right_face_lines_rawObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/grid_generator/megaminx/face_projection.rb', line 127

def top_right_face_lines_raw
  @top_right_face_lines_raw ||= (0..4).map do |i| 
    a = outside_pentagon_points[0][i-1] # offset by one so first line is top right
    b = outside_pentagon_points[0][(i)%5]
    c = outside_pentagon_points[0][(i+1)%5]
    d = outside_pentagon_points[0][(i+2)%5]

    ab_intervals = GridGenerator::Helper.intervals(a,b,2)
    cd_intervals = GridGenerator::Helper.intervals(c,d,2)

    GridGenerator::BaseLine.new(a: ab_intervals[-1], b: cd_intervals[0])
  end
end

#top_right_pentagon_pointsObject



96
97
98
99
100
101
102
103
104
# File 'lib/grid_generator/megaminx/face_projection.rb', line 96

def top_right_pentagon_points
  @top_right_pentagon_points ||= [
    decagon_points[1],
    decagon_points[2],
    pentagon_points[1],
    pentagon_points[0],
    decagon_points[0],
  ]
end