Class: GridGenerator::Pyraminx::TriangleFactory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, row:, col:, units:, size:, face:, rotator:, scaler:) ⇒ TriangleFactory

Returns a new instance of TriangleFactory.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 7

def initialize(x:, y:, row:, col:, units:, size:, face:, rotator:, scaler:)
  @x, @y = x, y
  @row = row
  @col = col
  @units = units
  @size = size
  face_attr = GridGenerator::FaceParser.new(face).parse
  @colour = face_attr && face_attr[:colour]
  @opacity = face_attr && face_attr[:opacity]
  @rotator = rotator
  @scaler = scaler
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def col
  @col
end

#colourObject (readonly)

Returns the value of attribute colour.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def colour
  @colour
end

#opacityObject (readonly)

Returns the value of attribute opacity.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def opacity
  @opacity
end

#rotatorObject (readonly)

Returns the value of attribute rotator.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def rotator
  @rotator
end

#rowObject (readonly)

Returns the value of attribute row.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def row
  @row
end

#scalerObject (readonly)

Returns the value of attribute scaler.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def scaler
  @scaler
end

#sizeObject (readonly)

Returns the value of attribute size.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def size
  @size
end

#unitsObject (readonly)

Returns the value of attribute units.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def units
  @units
end

#xObject (readonly)

Returns the value of attribute x.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



20
21
22
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 20

def y
  @y
end

Instance Method Details

#bottom_yObject

(0 + 1) * Math.sqrt(3)/2 * units # row = 0

(1 + 1) * Math.sqrt(3)/2 * units # row = 1
(2 + 1) * Math.sqrt(3)/2 * units # row = 2


66
67
68
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 66

def bottom_y
  (row + 1) * Math.sqrt(3)/2 * units
end

#buildObject



119
120
121
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 119

def build
  GridGenerator::BaseElement.new(points: points, colour: colour, opacity: opacity) unless colour.nil?
end

#even_bottom_left_xObject

x + (0 * -1 + (3 - 1)) * 0.5 * units + 0 * 0.5 * units # row = 0, col = 0 x + (1 * -1 + (3 - 1)) * 0.5 * units + 0 * 0.5 * units # row = 1, col = 0 x + (1 * -1 + (3 - 1)) * 0.5 * units + 2 * 0.5 * units # row = 1, col = 2 x + (2 * -1 + (3 - 1)) * 0.5 * units + 0 * 0.5 * units # row = 2, col = 0 x + (2 * -1 + (3 - 1)) * 0.5 * units + 2 * 0.5 * units # row = 2, col = 2 x + (2 * -1 + (3 - 1)) * 0.5 * units + 4 * 0.5 * units# row = 2, col = 4



42
43
44
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 42

def even_bottom_left_x
  (row * -1 + (size - 1)) * 0.5 * units + col * 0.5 * units
end

#even_bottom_right_xObject

x + (0 * -1 + (3 + 1)) * 0.5 * units + 0 * 0.5 * units # row = 0, col = 0

x + (1 * -1 + (3 + 1)) * 0.5 * units + 0 * 0.5 * units # row = 1, col = 0
x + (1 * -1 + (3 + 1)) * 0.5 * units + 2 * 0.5 * units # row = 1, col = 2 
x + (2 * -1 + (3 + 1)) * 0.5 * units + 0 * 0.5 * units # row = 2, col = 0
x + (2 * -1 + (3 + 1)) * 0.5 * units + 2 * 0.5 * units # row = 2, col = 2 
x + (2 * -1 + (3 + 1)) * 0.5 * units + 4 * 0.5 * units # row = 2, col = 4


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

def even_bottom_right_x
  (row * -1 + (size + 1)) * 0.5 * units + col * 0.5 * units
end

#even_pointsObject



95
96
97
98
99
100
101
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 95

def even_points
  [
    Matrix.column_vector([even_top_x, top_y]),
    Matrix.column_vector([even_bottom_right_x, bottom_y]),
    Matrix.column_vector([even_bottom_left_x, bottom_y])
  ]
end

#even_top_xObject

x + ((3 - 0) * 0.5 * units) + 0 * 0.5 * units # row = 0, col = 0

x + ((3 - 1) * 0.5 * units) + 0 * 0.5 * units # row = 1, col = 0
x + ((3 - 1) * 0.5 * units) + 2 * 0.5 * units # row = 1, col = 2 
x + ((3 - 2) * 0.5 * units) + 0 * 0.5 * units # row = 2, col = 0
x + ((3 - 2) * 0.5 * units) + 2 * 0.5 * units # row = 2, col = 2 
x + ((3 - 2) * 0.5 * units) + 4 * 0.5 * units # row = 2, col = 4


32
33
34
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 32

def even_top_x
  ((size - row) * 0.5 * units) + col * 0.5 * units
end

#odd_bottom_xObject

(0 * -1 + 3 + 1) * 0.5 * units + (1 - 1) * 0.5 * units # row 0, col = 1

(1 * -1 + 3 + 1) * 0.5 * units + (1 - 1) * 0.5 * units # row 1, col = 1
(2 * -1 + 3 + 1) * 0.5 * units + (1 - 1) * 0.5 * units # row 2, col = 1
(2 * -1 + 3 + 1) * 0.5 * units + (3 - 1) * 0.5 * units # row 2, col = 3


91
92
93
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 91

def odd_bottom_x
  (row * -1 + size + 1) * 0.5 * units + (col - 1) * 0.5 * units
end

#odd_pointsObject



103
104
105
106
107
108
109
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 103

def odd_points
  [
    Matrix.column_vector([odd_top_left_x, top_y]),
    Matrix.column_vector([odd_top_right_x, top_y]),
    Matrix.column_vector([odd_bottom_x, bottom_y])
  ]
end

#odd_top_left_xObject

(0 * -1 + 3) * 0.5 * units # row 0

(1 * -1 + 3) * 0.5 * units + (1 - 1) * 0.5 * units # row 1, col = 1 
(1 * -1 + 3) * 0.5 * units + (3 - 1) * 0.5 * units # row 1, col = 3 
(2 * -1 + 3) * 0.5 * units + (1 - 1) * 0.5 * units # row 2, col = 1
(2 * -1 + 3) * 0.5 * units + (3 - 1) * 0.5 * units # row 2, col = 3


75
76
77
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 75

def odd_top_left_x
  (row * -1 + size) * 0.5 * units + (col - 1) * 0.5 * units
end

#odd_top_right_xObject

((0 * -1) + 3 + 2) * 0.5 * units + (1 - 1) * 0.5 * units # row 0, col = 1 ((1 * -1) + 3 + 2) * 0.5 * units + (1 - 1) * 0.5 * units # row 1, col = 1 ((2 * -1) + 3 + 2) * 0.5 * units + (1 - 1) * 0.5 * units # row 2, col = 1 ((2 * -1) + 3 + 2) * 0.5 * units + (3 - 1) * 0.5 * units # row 2, col = 3



83
84
85
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 83

def odd_top_right_x
  (row * -1 + size + 2) * 0.5 * units + (col - 1) * 0.5 * units
end

#offsetObject



22
23
24
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 22

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

#pointsObject



111
112
113
114
115
116
117
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 111

def points
  all_points = col % 2 == 0 ? even_points : odd_points
  all_points.map do |point|
    scaled = scaler.scale(point)
    rotator.rotate(scaled) + offset
  end
end

#top_yObject

0 * Math.sqrt(3)/2 * units # row = 0

1 * Math.sqrt(3)/2 * units # row = 1
2 * Math.sqrt(3)/2 * units # row = 2


59
60
61
# File 'lib/grid_generator/pyraminx/triangle_factory.rb', line 59

def top_y
  row * Math.sqrt(3)/2 * units
end