grid_generator 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13f7a6b485fa1238581e0acf2470c1a4e8bfa934d01678636b074b516b95e814
4
- data.tar.gz: 9c26054a0b5e6e30aa17c058602881db49e5fafbdaaa60a7a8b6f80fa570affc
3
+ metadata.gz: b7fa51b1f90d6133fa000c7328f588af19ec0bf7c9985c72dc6c45f319844a04
4
+ data.tar.gz: cacef7a4e2db775e0a1eb16dc8216ca8d0aae914e5e0f6fd3c67cb688a774308
5
5
  SHA512:
6
- metadata.gz: 189c300178031b6bb9089cf2801787552dfc659c0c2ba42b41829a1331eb2d7b0d5966c2a39338d3965efed263b1a1e4c44f63aee94114aeddc66b21b0bc267e
7
- data.tar.gz: 4373a281c204c8955c68509510e691bc5947f4a705ffbc4d7e1b9a29fe3391aa13c4cee9072204daf31adb65c76eedc043356a66beb24b2eb2eb6e0f2d305dde
6
+ metadata.gz: bc5115584895296ae2ce85a8151cd6a0f47925e59e0d534c2167385d6df80a0bbe642f976515a23121662d844b86768538eb3587c0632394888894e52591ceb9
7
+ data.tar.gz: 3cac31b7d257093ed7a06a04c0e0e49bd245556ef5ffa5ccb40f649a67d9be056c1a156d3f32c9446ba673218541256ae07a3b0668cd3f46fbb1255622622d63
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grid_generator (0.1.10)
4
+ grid_generator (0.1.12)
5
5
  matrix (~> 0.4.2)
6
6
 
7
7
  GEM
@@ -1,4 +1,6 @@
1
1
  require_relative 'triangle_factory'
2
+ require_relative '../rotator'
3
+ require_relative '../scaler'
2
4
 
3
5
  module GridGenerator
4
6
  module Pyraminx
@@ -6,18 +8,39 @@ module GridGenerator
6
8
  # * * *
7
9
  # * * * * *
8
10
  class Face
9
- def initialize(x:, y:, units:, elements:)
11
+ def initialize(x:, y:, units:, elements:, vertical_scale: 1, rotation_angle: 0)
10
12
  @x, @y = x, y
11
13
  @units = units
12
14
  @elements = elements.split('\n').map { |r| r.split(',') }
15
+ @vertical_scale = vertical_scale
16
+ @rotation_angle = rotation_angle
13
17
  end
14
18
 
15
- attr_reader :x, :y, :units, :elements
19
+ attr_reader :x, :y, :units, :elements, :vertical_scale, :rotation_angle
16
20
 
17
21
  def size
18
22
  elements.size
19
23
  end
20
24
 
25
+ def centre_point
26
+ Matrix.column_vector([
27
+ size * units * 0.5,
28
+ size * Math.sqrt(3) * 0.25 * units
29
+ ])
30
+ end
31
+
32
+ def offset
33
+ Matrix.column_vector([x, y])
34
+ end
35
+
36
+ def rotator
37
+ @rotator ||= GridGenerator::Rotator.new(angle: rotation_angle, rotation_point: centre_point)
38
+ end
39
+
40
+ def scaler
41
+ @scaler ||= GridGenerator::Scaler.new(horizontal_scale: 1, vertical_scale: vertical_scale)
42
+ end
43
+
21
44
  def start_x_for_row(r)
22
45
  (size - 1 - r) * units / 2
23
46
  end
@@ -30,62 +53,134 @@ module GridGenerator
30
53
  Math.sqrt(3)/2 * (r + 1) * units
31
54
  end
32
55
 
56
+ def vertical_start_point_for_row(r)
57
+ Matrix.column_vector([
58
+ start_x_for_row(r),
59
+ y_for_row(r),
60
+ ])
61
+ end
62
+
63
+ def vertical_end_point_for_row(r)
64
+ Matrix.column_vector([
65
+ end_x_for_row(r),
66
+ y_for_row(r)
67
+ ])
68
+ end
69
+
33
70
  def vertical_lines
34
71
  Array.new(size-1) do |i|
72
+ point_1 = vertical_start_point_for_row(i)
73
+ point_2 = vertical_end_point_for_row(i)
74
+
75
+ scaled_1 = scaler.scale(point_1)
76
+ scaled_2 = scaler.scale(point_2)
77
+
78
+ transformed_1 = rotator.rotate(scaled_1) + offset
79
+ transformed_2 = rotator.rotate(scaled_2) + offset
80
+
35
81
  GridGenerator::BaseLine.new(
36
- x1: x + start_x_for_row(i),
37
- y1: y + y_for_row(i),
38
- x2: x + end_x_for_row(i),
39
- y2: y + y_for_row(i)
82
+ x1: transformed_1[0,0],
83
+ y1: transformed_1[1,0],
84
+ x2: transformed_2[0,0],
85
+ y2: transformed_2[1,0]
40
86
  )
41
87
  end
42
88
  end
43
89
 
90
+ def diagonal_down_start_point_for_row(r)
91
+ Matrix.column_vector([
92
+ start_x_for_row(r),
93
+ y_for_row(r),
94
+ ])
95
+ end
96
+
97
+ def diagonal_down_end_point_for_row(r)
98
+ Matrix.column_vector([
99
+ (size - 1 - r)*units,
100
+ y_for_row(size-1)
101
+ ])
102
+ end
103
+
44
104
  def diagonal_down_lines
45
105
  Array.new(size-1) do |i|
106
+ point_1 = diagonal_down_start_point_for_row(i)
107
+ point_2 = diagonal_down_end_point_for_row(i)
108
+
109
+ scaled_1 = scaler.scale(point_1)
110
+ scaled_2 = scaler.scale(point_2)
111
+
112
+ transformed_1 = rotator.rotate(scaled_1) + offset
113
+ transformed_2 = rotator.rotate(scaled_2) + offset
114
+
46
115
  GridGenerator::BaseLine.new(
47
- x1: x + start_x_for_row(i),
48
- y1: y + y_for_row(i),
49
- x2: x + (size - 1 - i)*units,
50
- y2: y + y_for_row(size-1)
116
+ x1: transformed_1[0,0],
117
+ y1: transformed_1[1,0],
118
+ x2: transformed_2[0,0],
119
+ y2: transformed_2[1,0]
51
120
  )
52
121
  end
53
122
  end
54
123
 
124
+ def diagonal_up_start_point_for_row(r)
125
+ Matrix.column_vector([
126
+ (r+1)*units,
127
+ y_for_row(size-1),
128
+ ])
129
+ end
130
+
131
+ def diagonal_up_end_point_for_row(r)
132
+ Matrix.column_vector([
133
+ end_x_for_row(r),
134
+ y_for_row(r)
135
+ ])
136
+ end
137
+
55
138
  def diagonal_up_lines
56
139
  Array.new(size-1) do |i|
140
+ point_1 = diagonal_up_start_point_for_row(i)
141
+ point_2 = diagonal_up_end_point_for_row(i)
142
+
143
+ scaled_1 = scaler.scale(point_1)
144
+ scaled_2 = scaler.scale(point_2)
145
+
146
+ transformed_1 = rotator.rotate(scaled_1) + offset
147
+ transformed_2 = rotator.rotate(scaled_2) + offset
148
+
57
149
  GridGenerator::BaseLine.new(
58
- x1: x + (i+1)*units,
59
- y1: y + y_for_row(size-1),
60
- x2: x + end_x_for_row(i),
61
- y2: y + y_for_row(i)
150
+ x1: transformed_1[0,0],
151
+ y1: transformed_1[1,0],
152
+ x2: transformed_2[0,0],
153
+ y2: transformed_2[1,0]
62
154
  )
63
155
  end
64
156
  end
65
157
 
66
158
  def top
67
159
  Matrix.column_vector([
68
- x + size * units / 2,
69
- y
160
+ size * units / 2,
161
+ 0
70
162
  ])
71
163
  end
72
164
 
73
165
  def bottom_left
74
166
  Matrix.column_vector([
75
- x,
76
- y + size * Math.sqrt(3)/2 * units
167
+ 0,
168
+ size * Math.sqrt(3)/2 * units
77
169
  ])
78
170
  end
79
171
 
80
172
  def bottom_right
81
173
  Matrix.column_vector([
82
- x + size * units,
83
- y + size * Math.sqrt(3)/2 * units
174
+ size * units,
175
+ size * Math.sqrt(3)/2 * units
84
176
  ])
85
177
  end
86
178
 
87
179
  def points
88
- [ top, bottom_left, bottom_right ]
180
+ [ top, bottom_left, bottom_right ].map do |point|
181
+ scaled = scaler.scale(point)
182
+ rotator.rotate(scaled) + offset
183
+ end
89
184
  end
90
185
 
91
186
  def points_string
@@ -102,7 +197,9 @@ module GridGenerator
102
197
  col: col_num,
103
198
  units: units,
104
199
  size: size,
105
- face: col
200
+ face: col,
201
+ rotator: rotator,
202
+ scaler: scaler
106
203
  ).build unless col == '-'
107
204
  end
108
205
  end.flatten.compact
@@ -4,7 +4,7 @@ require_relative '../base_element'
4
4
  module GridGenerator
5
5
  module Pyraminx
6
6
  class TriangleFactory
7
- def initialize(x:, y:, row:, col:, units:, size:, face:)
7
+ def initialize(x:, y:, row:, col:, units:, size:, face:, rotator:, scaler:)
8
8
  @x, @y = x, y
9
9
  @row = row
10
10
  @col = col
@@ -13,9 +13,15 @@ module GridGenerator
13
13
  face_attr = GridGenerator::FaceParser.new(face).parse
14
14
  @colour = face_attr && face_attr[:colour]
15
15
  @opacity = face_attr && face_attr[:opacity]
16
+ @rotator = rotator
17
+ @scaler = scaler
16
18
  end
17
19
 
18
- attr_reader :x, :y, :row, :col, :units, :size, :colour, :opacity
20
+ attr_reader :x, :y, :row, :col, :units, :size, :colour, :opacity, :rotator, :scaler
21
+
22
+ def offset
23
+ Matrix.column_vector([x, y])
24
+ end
19
25
 
20
26
  # x + ((3 - 0) * 0.5 * units) + 0 * 0.5 * units # row = 0, col = 0
21
27
  # x + ((3 - 1) * 0.5 * units) + 0 * 0.5 * units # row = 1, col = 0
@@ -24,7 +30,7 @@ module GridGenerator
24
30
  # x + ((3 - 2) * 0.5 * units) + 2 * 0.5 * units # row = 2, col = 2
25
31
  # x + ((3 - 2) * 0.5 * units) + 4 * 0.5 * units # row = 2, col = 4
26
32
  def even_top_x
27
- x + ((size - row) * 0.5 * units) + col * 0.5 * units
33
+ ((size - row) * 0.5 * units) + col * 0.5 * units
28
34
  end
29
35
 
30
36
  # x + (0 * -1 + (3 - 1)) * 0.5 * units + 0 * 0.5 * units # row = 0, col = 0
@@ -34,7 +40,7 @@ module GridGenerator
34
40
  # x + (2 * -1 + (3 - 1)) * 0.5 * units + 2 * 0.5 * units # row = 2, col = 2
35
41
  # x + (2 * -1 + (3 - 1)) * 0.5 * units + 4 * 0.5 * units# row = 2, col = 4
36
42
  def even_bottom_left_x
37
- x + (row * -1 + (size - 1)) * 0.5 * units + col * 0.5 * units
43
+ (row * -1 + (size - 1)) * 0.5 * units + col * 0.5 * units
38
44
  end
39
45
 
40
46
  # x + (0 * -1 + (3 + 1)) * 0.5 * units + 0 * 0.5 * units # row = 0, col = 0
@@ -44,21 +50,21 @@ module GridGenerator
44
50
  # x + (2 * -1 + (3 + 1)) * 0.5 * units + 2 * 0.5 * units # row = 2, col = 2
45
51
  # x + (2 * -1 + (3 + 1)) * 0.5 * units + 4 * 0.5 * units # row = 2, col = 4
46
52
  def even_bottom_right_x
47
- x + (row * -1 + (size + 1)) * 0.5 * units + col * 0.5 * units
53
+ (row * -1 + (size + 1)) * 0.5 * units + col * 0.5 * units
48
54
  end
49
55
 
50
56
  # 0 * Math.sqrt(3)/2 * units # row = 0
51
57
  # 1 * Math.sqrt(3)/2 * units # row = 1
52
58
  # 2 * Math.sqrt(3)/2 * units # row = 2
53
59
  def top_y
54
- y + row * Math.sqrt(3)/2 * units
60
+ row * Math.sqrt(3)/2 * units
55
61
  end
56
62
 
57
63
  # (0 + 1) * Math.sqrt(3)/2 * units # row = 0
58
64
  # (1 + 1) * Math.sqrt(3)/2 * units # row = 1
59
65
  # (2 + 1) * Math.sqrt(3)/2 * units # row = 2
60
66
  def bottom_y
61
- y + (row + 1) * Math.sqrt(3)/2 * units
67
+ (row + 1) * Math.sqrt(3)/2 * units
62
68
  end
63
69
 
64
70
  # (0 * -1 + 3) * 0.5 * units # row 0
@@ -67,7 +73,7 @@ module GridGenerator
67
73
  # (2 * -1 + 3) * 0.5 * units + (1 - 1) * 0.5 * units # row 2, col = 1
68
74
  # (2 * -1 + 3) * 0.5 * units + (3 - 1) * 0.5 * units # row 2, col = 3
69
75
  def odd_top_left_x
70
- x + (row * -1 + size) * 0.5 * units + (col - 1) * 0.5 * units
76
+ (row * -1 + size) * 0.5 * units + (col - 1) * 0.5 * units
71
77
  end
72
78
 
73
79
  # ((0 * -1) + 3 + 2) * 0.5 * units + (1 - 1) * 0.5 * units # row 0, col = 1
@@ -75,7 +81,7 @@ module GridGenerator
75
81
  # ((2 * -1) + 3 + 2) * 0.5 * units + (1 - 1) * 0.5 * units # row 2, col = 1
76
82
  # ((2 * -1) + 3 + 2) * 0.5 * units + (3 - 1) * 0.5 * units # row 2, col = 3
77
83
  def odd_top_right_x
78
- x + (row * -1 + size + 2) * 0.5 * units + (col - 1) * 0.5 * units
84
+ (row * -1 + size + 2) * 0.5 * units + (col - 1) * 0.5 * units
79
85
  end
80
86
 
81
87
  # (0 * -1 + 3 + 1) * 0.5 * units + (1 - 1) * 0.5 * units # row 0, col = 1
@@ -83,7 +89,7 @@ module GridGenerator
83
89
  # (2 * -1 + 3 + 1) * 0.5 * units + (1 - 1) * 0.5 * units # row 2, col = 1
84
90
  # (2 * -1 + 3 + 1) * 0.5 * units + (3 - 1) * 0.5 * units # row 2, col = 3
85
91
  def odd_bottom_x
86
- x + (row * -1 + size + 1) * 0.5 * units + (col - 1) * 0.5 * units
92
+ (row * -1 + size + 1) * 0.5 * units + (col - 1) * 0.5 * units
87
93
  end
88
94
 
89
95
  def even_points
@@ -103,10 +109,10 @@ module GridGenerator
103
109
  end
104
110
 
105
111
  def points
106
- if col % 2 == 0
107
- even_points
108
- else
109
- odd_points
112
+ all_points = col % 2 == 0 ? even_points : odd_points
113
+ all_points.map do |point|
114
+ scaled = scaler.scale(point)
115
+ rotator.rotate(scaled) + offset
110
116
  end
111
117
  end
112
118
 
@@ -0,0 +1,19 @@
1
+ module GridGenerator
2
+ class Rotator
3
+ def initialize(angle:, rotation_point:)
4
+ @angle = angle
5
+ @matrix = Matrix[
6
+ [Math.cos(angle), -1*Math.sin(angle)],
7
+ [Math.sin(angle), Math.cos(angle)]
8
+ ]
9
+ @rotation_point = rotation_point
10
+ end
11
+
12
+ attr_reader :angle, :matrix, :rotation_point
13
+
14
+ def rotate(point)
15
+ # subtract rotation point to move point towards 0,0, rotate, then add to move back
16
+ (matrix * (point - rotation_point)) + rotation_point
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module GridGenerator
2
+ class Scaler
3
+ def initialize(horizontal_scale: 1, vertical_scale: 1)
4
+ @horizontal_scale = horizontal_scale
5
+ @vertical_scale = vertical_scale
6
+ @matrix = Matrix[
7
+ [horizontal_scale,0],
8
+ [0,vertical_scale]
9
+ ]
10
+ end
11
+
12
+ attr_reader :horizontal_scale, :vertical_scale, :matrix
13
+
14
+ def scale(point)
15
+ matrix * point
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GridGenerator
4
- VERSION = "0.1.10"
4
+ VERSION = "0.1.12"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grid_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Humphreys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-02 00:00:00.000000000 Z
11
+ date: 2023-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: matrix
@@ -65,6 +65,8 @@ files:
65
65
  - lib/grid_generator/pyraminx/grid.rb
66
66
  - lib/grid_generator/pyraminx/triangle_factory.rb
67
67
  - lib/grid_generator/pyraminx/triangle_factory_v1.rb
68
+ - lib/grid_generator/rotator.rb
69
+ - lib/grid_generator/scaler.rb
68
70
  - lib/grid_generator/skewb/left_element_factory.rb
69
71
  - lib/grid_generator/skewb/left_skewb_grid.rb
70
72
  - lib/grid_generator/skewb/right_element_factory.rb