Class: GridGenerator::Pyraminx::Face

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/pyraminx/face.rb

Overview

*

    • *

        • *

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, units:, elements:, vertical_scale: 1, rotation_angle: 0) ⇒ Face

Returns a new instance of Face.



19
20
21
22
23
24
25
# File 'lib/grid_generator/pyraminx/face.rb', line 19

def initialize(x:, y:, units:, elements:, vertical_scale: 1, rotation_angle: 0)
  @x, @y = x, y
  @units = units
  @elements = elements.split('\n').map { |r| r.split(',') }
  @vertical_scale = vertical_scale 
  @rotation_angle = rotation_angle
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



27
28
29
# File 'lib/grid_generator/pyraminx/face.rb', line 27

def elements
  @elements
end

#rotation_angleObject (readonly)

Returns the value of attribute rotation_angle.



27
28
29
# File 'lib/grid_generator/pyraminx/face.rb', line 27

def rotation_angle
  @rotation_angle
end

#unitsObject (readonly)

Returns the value of attribute units.



27
28
29
# File 'lib/grid_generator/pyraminx/face.rb', line 27

def units
  @units
end

#vertical_scaleObject (readonly)

Returns the value of attribute vertical_scale.



27
28
29
# File 'lib/grid_generator/pyraminx/face.rb', line 27

def vertical_scale
  @vertical_scale
end

#xObject (readonly)

Returns the value of attribute x.



27
28
29
# File 'lib/grid_generator/pyraminx/face.rb', line 27

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



27
28
29
# File 'lib/grid_generator/pyraminx/face.rb', line 27

def y
  @y
end

Instance Method Details

#base_shapeObject



201
202
203
# File 'lib/grid_generator/pyraminx/face.rb', line 201

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

#base_shape_styleObject



197
198
199
# File 'lib/grid_generator/pyraminx/face.rb', line 197

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

#bottom_leftObject



158
159
160
161
162
163
# File 'lib/grid_generator/pyraminx/face.rb', line 158

def bottom_left
  Matrix.column_vector([
    0,
    size * Math.sqrt(3)/2 * units
  ])
end

#bottom_rightObject



165
166
167
168
169
170
# File 'lib/grid_generator/pyraminx/face.rb', line 165

def bottom_right
  Matrix.column_vector([
    size * units,
    size * Math.sqrt(3)/2 * units
  ])
end

#centre_pointObject



33
34
35
36
37
38
# File 'lib/grid_generator/pyraminx/face.rb', line 33

def centre_point
  Matrix.column_vector([
    size * units * 0.5,
    size * Math.sqrt(3) * 0.25 * units
  ])
end

#diagonal_down_end_point_for_row(r) ⇒ Object



100
101
102
103
104
105
# File 'lib/grid_generator/pyraminx/face.rb', line 100

def diagonal_down_end_point_for_row(r)
  Matrix.column_vector([
    (size - 1 - r)*units,
    y_for_row(size-1)
  ])
end

#diagonal_down_linesObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/grid_generator/pyraminx/face.rb', line 107

def diagonal_down_lines 
  Array.new(size-1) do |i|
    point_1 = diagonal_down_start_point_for_row(i)
    point_2 = diagonal_down_end_point_for_row(i)

    scaled_1 = scaler.scale(point_1)
    scaled_2 = scaler.scale(point_2)

    transformed_1 = rotator.rotate(scaled_1) + offset
    transformed_2 = rotator.rotate(scaled_2) + offset

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

#diagonal_down_start_point_for_row(r) ⇒ Object



93
94
95
96
97
98
# File 'lib/grid_generator/pyraminx/face.rb', line 93

def diagonal_down_start_point_for_row(r)
  Matrix.column_vector([
    start_x_for_row(r),
    y_for_row(r),
  ])
end

#diagonal_up_end_point_for_row(r) ⇒ Object



129
130
131
132
133
134
# File 'lib/grid_generator/pyraminx/face.rb', line 129

def diagonal_up_end_point_for_row(r)
  Matrix.column_vector([
    end_x_for_row(r),
    y_for_row(r)
  ])
end

#diagonal_up_linesObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/grid_generator/pyraminx/face.rb', line 136

def diagonal_up_lines 
  Array.new(size-1) do |i|
    point_1 = diagonal_up_start_point_for_row(i)
    point_2 = diagonal_up_end_point_for_row(i)

    scaled_1 = scaler.scale(point_1)
    scaled_2 = scaler.scale(point_2)

    transformed_1 = rotator.rotate(scaled_1) + offset
    transformed_2 = rotator.rotate(scaled_2) + offset

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

#diagonal_up_start_point_for_row(r) ⇒ Object



122
123
124
125
126
127
# File 'lib/grid_generator/pyraminx/face.rb', line 122

def diagonal_up_start_point_for_row(r)
  Matrix.column_vector([
    (r+1)*units,
    y_for_row(size-1),
  ])
end

#element_shapesObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/grid_generator/pyraminx/face.rb', line 179

def element_shapes
  elements.map.each_with_index do |row, row_num|
    row.map.each_with_index do |col, col_num|
      GridGenerator::Pyraminx::TriangleFactory.new(
        x: x,
        y: y,
        row: row_num,
        col: col_num,
        units: units,
        size: size,
        face: col,
        rotator: rotator,
        scaler: scaler
      ).build unless col == '-' 
    end
  end.flatten.compact
end

#end_x_for_row(r) ⇒ Object



56
57
58
# File 'lib/grid_generator/pyraminx/face.rb', line 56

def end_x_for_row(r)
  (size + 1 + r) * units / 2
end

#offsetObject



40
41
42
# File 'lib/grid_generator/pyraminx/face.rb', line 40

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

#pointsObject



172
173
174
175
176
177
# File 'lib/grid_generator/pyraminx/face.rb', line 172

def points
  [ top, bottom_left, bottom_right ].map do |point|
    scaled = scaler.scale(point)
    rotator.rotate(scaled) + offset
  end
end

#rotatorObject



44
45
46
# File 'lib/grid_generator/pyraminx/face.rb', line 44

def rotator
  @rotator ||= GridGenerator::Rotator.new(angle: rotation_angle, rotation_point: centre_point)
end

#scalerObject



48
49
50
# File 'lib/grid_generator/pyraminx/face.rb', line 48

def scaler
  @scaler ||= GridGenerator::Scaler.new(horizontal_scale: 1, vertical_scale: vertical_scale)
end

#sizeObject



29
30
31
# File 'lib/grid_generator/pyraminx/face.rb', line 29

def size
  elements.size
end

#start_x_for_row(r) ⇒ Object



52
53
54
# File 'lib/grid_generator/pyraminx/face.rb', line 52

def start_x_for_row(r)
  (size - 1 - r) * units / 2
end

#to_svgObject



205
206
207
208
209
210
211
212
213
214
# File 'lib/grid_generator/pyraminx/face.rb', line 205

def to_svg
  output = base_shape.to_svg

  vertical_lines.each { |line| output += line.to_svg }
  diagonal_up_lines.each { |line| output += line.to_svg }
  diagonal_down_lines.each { |line| output += line.to_svg }
  element_shapes.each { |shape| output += shape.to_svg }

  output
end

#topObject



151
152
153
154
155
156
# File 'lib/grid_generator/pyraminx/face.rb', line 151

def top
  Matrix.column_vector([
    size * units / 2,
    0
  ])
end

#vertical_end_point_for_row(r) ⇒ Object



71
72
73
74
75
76
# File 'lib/grid_generator/pyraminx/face.rb', line 71

def vertical_end_point_for_row(r)
  Matrix.column_vector([
    end_x_for_row(r),
    y_for_row(r)
  ])
end

#vertical_linesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/grid_generator/pyraminx/face.rb', line 78

def vertical_lines 
  Array.new(size-1) do |i|
    point_1 = vertical_start_point_for_row(i)
    point_2 = vertical_end_point_for_row(i)

    scaled_1 = scaler.scale(point_1)
    scaled_2 = scaler.scale(point_2)

    transformed_1 = rotator.rotate(scaled_1) + offset
    transformed_2 = rotator.rotate(scaled_2) + offset

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

#vertical_start_point_for_row(r) ⇒ Object



64
65
66
67
68
69
# File 'lib/grid_generator/pyraminx/face.rb', line 64

def vertical_start_point_for_row(r)
  Matrix.column_vector([
    start_x_for_row(r),
    y_for_row(r),
  ])
end

#y_for_row(r) ⇒ Object



60
61
62
# File 'lib/grid_generator/pyraminx/face.rb', line 60

def y_for_row(r)
  Math.sqrt(3)/2 * (r + 1) * units
end