grid_generator 0.1.0

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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/Gemfile +10 -0
  5. data/Gemfile.lock +23 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +39 -0
  8. data/Rakefile +12 -0
  9. data/grid_generator.gemspec +36 -0
  10. data/lib/grid_generator/arrows/arrow.rb +29 -0
  11. data/lib/grid_generator/arrows/diagonal_down_arrow.rb +107 -0
  12. data/lib/grid_generator/arrows/diagonal_up_arrow.rb +108 -0
  13. data/lib/grid_generator/arrows/horizontal_arrow.rb +108 -0
  14. data/lib/grid_generator/arrows/vertical_arrow.rb +108 -0
  15. data/lib/grid_generator/base_element.rb +30 -0
  16. data/lib/grid_generator/cubic/bordered_grid.rb +129 -0
  17. data/lib/grid_generator/cubic/facing_grid.rb +109 -0
  18. data/lib/grid_generator/cubic/facing_square_factory.rb +38 -0
  19. data/lib/grid_generator/cubic/front_grid.rb +27 -0
  20. data/lib/grid_generator/cubic/grid.rb +146 -0
  21. data/lib/grid_generator/cubic/iso_view.rb +30 -0
  22. data/lib/grid_generator/cubic/right_grid.rb +27 -0
  23. data/lib/grid_generator/cubic/square_factory.rb +46 -0
  24. data/lib/grid_generator/cubic/top_grid.rb +27 -0
  25. data/lib/grid_generator/face_parser.rb +43 -0
  26. data/lib/grid_generator/megaminx/common.rb +94 -0
  27. data/lib/grid_generator/megaminx/element_factory.rb +52 -0
  28. data/lib/grid_generator/megaminx/face.rb +79 -0
  29. data/lib/grid_generator/pyraminx/grid.rb +83 -0
  30. data/lib/grid_generator/pyraminx/triangle_factory.rb +45 -0
  31. data/lib/grid_generator/skewb/left_element_factory.rb +90 -0
  32. data/lib/grid_generator/skewb/left_skewb_grid.rb +43 -0
  33. data/lib/grid_generator/skewb/right_element_factory.rb +90 -0
  34. data/lib/grid_generator/skewb/right_skewb_grid.rb +43 -0
  35. data/lib/grid_generator/skewb/skewb_grid.rb +62 -0
  36. data/lib/grid_generator/skewb/top_element_factory.rb +90 -0
  37. data/lib/grid_generator/skewb/top_skewb_grid.rb +43 -0
  38. data/lib/grid_generator/square_one/element.rb +60 -0
  39. data/lib/grid_generator/square_one/element_factory.rb +89 -0
  40. data/lib/grid_generator/square_one/face.rb +82 -0
  41. data/lib/grid_generator/square_one_face_parser.rb +61 -0
  42. data/lib/grid_generator/version.rb +5 -0
  43. data/lib/grid_generator.rb +107 -0
  44. data/sig/grid_generator.rbs +4 -0
  45. metadata +103 -0
@@ -0,0 +1,108 @@
1
+ require './lib/grid_generator/arrows/arrow.rb'
2
+
3
+ module GridGenerator
4
+ module Arrows
5
+ class VerticalArrow < Arrow
6
+ ARROW_WIDTH = 16
7
+ ARROW_LENGTH = 8
8
+ LINE_WIDTH = 6
9
+
10
+ def arrow_start_point
11
+ [
12
+ [
13
+ x,
14
+ y+ARROW_LENGTH
15
+ ],
16
+ [
17
+ x+(ARROW_WIDTH/2),
18
+ y
19
+ ],
20
+ [
21
+ x+ARROW_WIDTH,
22
+ y+ARROW_LENGTH
23
+ ]
24
+ ]
25
+ end
26
+
27
+ def arrow_start_flat
28
+ [
29
+ [
30
+ x+(ARROW_WIDTH/2)-(LINE_WIDTH/2),
31
+ y
32
+ ],
33
+ [
34
+ x+(ARROW_WIDTH/2)+(LINE_WIDTH/2),
35
+ y
36
+ ]
37
+ ]
38
+ end
39
+
40
+ def arrow_start_side
41
+ [
42
+ [
43
+ x+(ARROW_WIDTH/2)+(LINE_WIDTH/2),
44
+ y+ARROW_LENGTH
45
+ ],
46
+ [
47
+ x+(ARROW_WIDTH/2)+(LINE_WIDTH/2),
48
+ y+ARROW_LENGTH+length
49
+ ]
50
+ ]
51
+ end
52
+
53
+ def arrow_end_point
54
+ [
55
+ [
56
+ x+ARROW_WIDTH,
57
+ y+ARROW_LENGTH+length
58
+ ],
59
+ [
60
+ x+(ARROW_WIDTH/2),
61
+ y+(ARROW_LENGTH*2)+length
62
+ ],
63
+ [
64
+ x,
65
+ y+ARROW_LENGTH+length
66
+ ]
67
+ ]
68
+ end
69
+
70
+ def arrow_end_flat
71
+ [
72
+ [
73
+ x+(ARROW_WIDTH/2)+(LINE_WIDTH/2),
74
+ y+(2*ARROW_LENGTH)+length
75
+ ],
76
+ [
77
+ x+(ARROW_WIDTH/2)-(LINE_WIDTH/2),
78
+ y+(2*ARROW_LENGTH)+length
79
+ ]
80
+ ]
81
+ end
82
+
83
+ def arrow_end_side
84
+ [
85
+ [
86
+ x+(ARROW_WIDTH/2)-(LINE_WIDTH/2),
87
+ y+ARROW_LENGTH+length
88
+ ],
89
+ [
90
+ x+(ARROW_WIDTH/2)-(LINE_WIDTH/2),
91
+ y+ARROW_LENGTH
92
+ ]
93
+ ]
94
+ end
95
+
96
+ def points
97
+ case direction
98
+ when :forward
99
+ arrow_start_flat + arrow_start_side + arrow_end_point + arrow_end_side
100
+ when :backward
101
+ arrow_start_point + arrow_start_side + arrow_end_flat + arrow_end_side
102
+ else
103
+ arrow_start_point + arrow_start_side + arrow_end_point + arrow_end_side
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,30 @@
1
+ module GridGenerator
2
+ class BaseElement
3
+ def initialize(points:, colour: , opacity: 1)
4
+ @points = points
5
+ @colour = colour
6
+ @opacity = opacity
7
+ end
8
+
9
+ attr_reader :points, :colour, :opacity
10
+
11
+ def ==(other)
12
+ self.class == other.class &&
13
+ self.points == other.points &&
14
+ self.colour == other.colour &&
15
+ self.opacity == other.opacity
16
+ end
17
+
18
+ def points_string
19
+ points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
20
+ end
21
+
22
+ def as_json
23
+ {
24
+ "points_string" => points_string,
25
+ "colour" => colour,
26
+ "opacity" => opacity
27
+ }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,129 @@
1
+ require './lib/grid_generator/face_parser'
2
+ require './lib/grid_generator/cubic/facing_square_factory'
3
+
4
+ module GridGenerator
5
+ module Cubic
6
+ class BorderedGrid
7
+ def initialize(x:, y:, units:, squares: )
8
+ @x, @y = x, y
9
+ @units = units
10
+ @squares = case squares
11
+ when String
12
+ FaceParser.new(squares).to_a
13
+ when Array
14
+ squares
15
+ else
16
+ raise ArgumentError, "squares must be array or string"
17
+ end
18
+ end
19
+
20
+ attr_reader :x, :y, :units, :squares
21
+
22
+ def width
23
+ squares.first.size
24
+ end
25
+
26
+ def height
27
+ squares.size
28
+ end
29
+
30
+ def border_unit
31
+ units / 4
32
+ end
33
+
34
+ def max_x
35
+ x + (width - 2) * units + (2 * border_unit)
36
+ end
37
+
38
+ def max_y
39
+ y + (height - 2) * units + (2 * border_unit)
40
+ end
41
+
42
+ def unit_x(n)
43
+ if n == 0
44
+ x
45
+ else
46
+ x + border_unit + (n - 1) * units
47
+ end
48
+ end
49
+
50
+ def unit_y(n)
51
+ if n == 0
52
+ y
53
+ else
54
+ y + border_unit + (n - 1) * units
55
+ end
56
+ end
57
+
58
+ def unit_width(n)
59
+ if n == 0 || n == (width - 1)
60
+ border_unit
61
+ else
62
+ units
63
+ end
64
+ end
65
+
66
+ def unit_height(n)
67
+ if n == 0 || n == (height - 1)
68
+ border_unit
69
+ else
70
+ units
71
+ end
72
+ end
73
+
74
+ def rows
75
+ Array.new(height) do |i|
76
+ {
77
+ "x1" => x,
78
+ "y1" => unit_y(i+1),
79
+ "x2" => max_x,
80
+ "y2" => unit_y(i+1)
81
+ }
82
+ end
83
+ end
84
+
85
+ def columns
86
+ Array.new(width) do |i|
87
+ {
88
+ "x1" => unit_x(i+1),
89
+ "y1" => y,
90
+ "x2" => unit_x(i+1),
91
+ "y2" => max_y
92
+ }
93
+ end
94
+ end
95
+
96
+ def element_shapes
97
+ squares.map.each_with_index do |row, row_num|
98
+ row.map.each_with_index do |col, col_num|
99
+ if col
100
+ 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
101
+ end
102
+ end
103
+ end.flatten.compact
104
+ end
105
+
106
+ def points
107
+ [
108
+ Matrix.column_vector([ x, y ]),
109
+ Matrix.column_vector([ max_x, y ]),
110
+ Matrix.column_vector([ max_x, max_y ]),
111
+ Matrix.column_vector([ x, max_y ])
112
+ ]
113
+ end
114
+
115
+ def points_string
116
+ points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
117
+ end
118
+
119
+ def as_json
120
+ {
121
+ "points_string" => points_string,
122
+ "rows" => rows,
123
+ "columns" => columns,
124
+ "element_shapes" => element_shapes.map(&:as_json)
125
+ }
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,109 @@
1
+ require './lib/grid_generator/face_parser'
2
+ require './lib/grid_generator/cubic/facing_square_factory'
3
+
4
+ module GridGenerator
5
+ module Cubic
6
+ class FacingGrid
7
+ def initialize(x:, y:, units:, squares: )
8
+ @x, @y = x, y
9
+ @units = units
10
+ @squares = case squares
11
+ when String
12
+ FaceParser.new(squares).to_a
13
+ when Array
14
+ squares
15
+ else
16
+ raise ArgumentError, "squares must be array or string"
17
+ end
18
+ end
19
+
20
+ attr_reader :x, :y, :units, :squares
21
+
22
+ def width
23
+ squares.first.size
24
+ end
25
+
26
+ def height
27
+ squares.size
28
+ end
29
+
30
+ def max_x
31
+ x + width * units
32
+ end
33
+
34
+ def max_y
35
+ y + height * units
36
+ end
37
+
38
+ def unit_x(n)
39
+ x + n * units
40
+ end
41
+
42
+ def unit_y(n)
43
+ y + n * units
44
+ end
45
+
46
+ def unit_width(_)
47
+ units
48
+ end
49
+
50
+ def unit_height(_)
51
+ units
52
+ end
53
+
54
+ def rows
55
+ Array.new(height) do |i|
56
+ {
57
+ "x1" => x,
58
+ "y1" => unit_y(i+1),
59
+ "x2" => max_x,
60
+ "y2" => unit_y(i+1)
61
+ }
62
+ end
63
+ end
64
+
65
+ def columns
66
+ Array.new(width) do |i|
67
+ {
68
+ "x1" => unit_x(i+1),
69
+ "y1" => y,
70
+ "x2" => unit_x(i+1),
71
+ "y2" => max_y
72
+ }
73
+ end
74
+ end
75
+
76
+ def element_shapes
77
+ squares.map.each_with_index do |row, row_num|
78
+ row.map.each_with_index do |col, col_num|
79
+ if col
80
+ 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
81
+ end
82
+ end
83
+ end.flatten.compact
84
+ end
85
+
86
+ def points
87
+ [
88
+ Matrix.column_vector([ x, y ]),
89
+ Matrix.column_vector([ max_x, y ]),
90
+ Matrix.column_vector([ max_x, max_y ]),
91
+ Matrix.column_vector([ x, max_y ])
92
+ ]
93
+ end
94
+
95
+ def points_string
96
+ points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
97
+ end
98
+
99
+ def as_json
100
+ {
101
+ "points_string" => points_string,
102
+ "rows" => rows,
103
+ "columns" => columns,
104
+ "element_shapes" => element_shapes.map(&:as_json)
105
+ }
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,38 @@
1
+ require './lib/grid_generator/base_element'
2
+
3
+ module GridGenerator
4
+ module Cubic
5
+ class FacingSquareFactory
6
+ def initialize(x: , y:, width:, height:, colour:, opacity:)
7
+ @x, @y = x, y
8
+ @width, @height = width, height
9
+ @colour, @opacity = colour, opacity
10
+ end
11
+
12
+ attr_reader :x, :y, :width, :height, :colour, :opacity
13
+
14
+ def max_x
15
+ x + width
16
+ end
17
+
18
+ def max_y
19
+ y + height
20
+ end
21
+
22
+ def points
23
+ [
24
+ [ x, y ],
25
+ [ max_x, y ],
26
+ [ max_x, max_y ],
27
+ [ x, max_y ]
28
+ ]
29
+ end
30
+
31
+ def build
32
+ GridGenerator::BaseElement.new(points: points, colour: colour, opacity: opacity)
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
@@ -0,0 +1,27 @@
1
+ require './lib/grid_generator/cubic/grid'
2
+
3
+ module GridGenerator
4
+ module Cubic
5
+ class FrontGrid < GridGenerator::Cubic::Grid
6
+ def width_unit
7
+ @width_unit ||= Matrix.column_vector([units, units/2])
8
+ end
9
+
10
+ def height_unit
11
+ @height_unit ||= Matrix.column_vector([0, units])
12
+ end
13
+
14
+ def offset_unit
15
+ @offset_unit ||= Matrix.column_vector([0,0])
16
+ end
17
+
18
+ def line_offset_amount
19
+ 0
20
+ end
21
+
22
+ def square_offset_amount
23
+ 0
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,146 @@
1
+ require 'matrix'
2
+ require './lib/grid_generator/face_parser'
3
+
4
+ module GridGenerator
5
+ module Cubic
6
+ class Grid
7
+ def initialize(x:, y:, units: , squares: )
8
+ @x, @y = x, y
9
+ @units = units
10
+ @squares = case squares
11
+ when String
12
+ FaceParser.new(squares).to_a
13
+ when Array
14
+ squares
15
+ else
16
+ raise ArgumentError, "squares must be array or string"
17
+ end
18
+ end
19
+
20
+ attr_reader :x, :y, :units, :squares
21
+
22
+ def ==(other)
23
+ self.x == other.x &&
24
+ self.y == other.y &&
25
+ self.units == other.units &&
26
+ self.squares == other.squares
27
+ end
28
+
29
+ def width
30
+ @width ||= squares.first.size
31
+ end
32
+
33
+ def height
34
+ @height ||= squares.size
35
+ end
36
+
37
+ # positioning squares
38
+
39
+ def top_left_square
40
+ @top_left_square ||= Matrix.column_vector([x, y]) + offset_unit*square_offset_amount
41
+ end
42
+
43
+ def square_position(row, col)
44
+ top_left_square + width_unit*col + height_unit*row
45
+ end
46
+
47
+ # positioning lines
48
+
49
+ def top_left
50
+ @top_left ||= Matrix.column_vector([x,y]) + offset_unit*line_offset_amount
51
+ end
52
+
53
+ def top_right
54
+ @top_right ||= top_left + width_unit*width
55
+ end
56
+
57
+ def bottom_right
58
+ @bottom_right ||= top_left + width_unit*width + height_unit*height
59
+ end
60
+
61
+ def bottom_left
62
+ @bottom_left ||= top_left + height_unit*height
63
+ end
64
+
65
+ def row_line_start(n)
66
+ top_left + height_unit*n
67
+ end
68
+
69
+ def row_line_end(n)
70
+ top_right + height_unit*n
71
+ end
72
+
73
+ def column_line_start(n)
74
+ top_left + width_unit*n
75
+ end
76
+
77
+ def column_line_end(n)
78
+ bottom_left + width_unit*n
79
+ end
80
+
81
+ def rows
82
+ Array.new(height) do |i|
83
+ {
84
+ "x1" => row_line_start(i+1)[0,0],
85
+ "y1" => row_line_start(i+1)[1,0],
86
+ "x2" => row_line_end(i+1)[0,0],
87
+ "y2" => row_line_end(i+1)[1,0]
88
+ }
89
+ end
90
+ end
91
+
92
+ def columns
93
+ Array.new(width) do |i|
94
+ {
95
+ "x1" => column_line_start(i+1)[0,0],
96
+ "y1" => column_line_start(i+1)[1,0],
97
+ "x2" => column_line_end(i+1)[0,0],
98
+ "y2" => column_line_end(i+1)[1,0]
99
+ }
100
+ end
101
+ end
102
+
103
+ def element_shapes
104
+ squares.map.each_with_index do |row, row_num|
105
+ row.map.each_with_index do |col, col_num|
106
+ if col
107
+ GridGenerator::Cubic::SquareFactory.new(
108
+ x: square_position(row_num, col_num)[0,0],
109
+ y: square_position(row_num, col_num)[1,0],
110
+ units: units,
111
+ width_unit: width_unit,
112
+ height_unit: height_unit,
113
+ offset_unit: offset_unit,
114
+ colour: col[:colour],
115
+ opacity: col[:opacity]
116
+ ).build
117
+ end
118
+ end
119
+ end.flatten.compact
120
+ end
121
+
122
+ def points
123
+ [
124
+ top_left,
125
+ top_right,
126
+ bottom_right,
127
+ bottom_left
128
+ ]
129
+ end
130
+
131
+ def points_string
132
+ points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
133
+ end
134
+
135
+ def as_json
136
+ {
137
+ "points_string" => points_string,
138
+ "rows" => rows,
139
+ "columns" => columns,
140
+ "element_shapes" => element_shapes.map(&:as_json)
141
+ }
142
+ end
143
+ end
144
+ end
145
+ end
146
+
@@ -0,0 +1,30 @@
1
+ module GridGenerator
2
+ module Cubic
3
+ class IsoView
4
+ def initialize(x: , y: , units: , top_squares: , front_squares: , right_squares:)
5
+ @x, @y = x, y
6
+ @units = units
7
+ top_x = x
8
+ top_y = y
9
+ @top = GridGenerator::Cubic::TopGrid.new(x: top_x, y: top_y, units: units, squares: top_squares)
10
+ front_x = x
11
+ front_y = y + top.squares.size * units / 2 # y + (top.squares.size * units / 2)
12
+ @front = GridGenerator::Cubic::FrontGrid.new(x: front_x, y: front_y, units: units, squares: front_squares)
13
+
14
+ right_x = x + top.squares.first.size * units
15
+ right_y = y + top.squares.first.size * units / 2
16
+ @right = GridGenerator::Cubic::RightGrid.new(x: right_x, y: right_y, units: units, squares: right_squares)
17
+ end
18
+
19
+ attr_reader :x, :y, :units, :top, :front, :right
20
+
21
+ def as_json
22
+ {
23
+ "top" => top.as_json,
24
+ "front" => front.as_json,
25
+ "right" => right.as_json
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ require './lib/grid_generator/cubic/grid'
2
+
3
+ module GridGenerator
4
+ module Cubic
5
+ class RightGrid < Cubic::Grid
6
+ def width_unit
7
+ @width_unit ||= Matrix.column_vector([units, (units*-0.5).to_i])
8
+ end
9
+
10
+ def height_unit
11
+ @height_unit ||= Matrix.column_vector([0, units])
12
+ end
13
+
14
+ def offset_unit
15
+ @offset_unit ||= Matrix.column_vector([0,(units*0.5).to_i])
16
+ end
17
+
18
+ def line_offset_amount
19
+ width
20
+ end
21
+
22
+ def square_offset_amount
23
+ width - 1
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ require './lib/grid_generator/base_element'
2
+
3
+ module GridGenerator
4
+ module Cubic
5
+ class SquareFactory
6
+ def initialize(x:, y:, units:, width_unit:, height_unit:, offset_unit:, colour:, opacity:)
7
+ @x, @y = x, y
8
+ @units = units
9
+ @width_unit, @height_unit = width_unit, height_unit
10
+ @offset_unit = offset_unit
11
+ @colour, @opacity = colour, opacity
12
+ end
13
+
14
+ attr_reader :x, :y, :units, :width_unit, :height_unit, :offset_unit, :colour, :opacity
15
+
16
+ def top_left
17
+ Matrix.column_vector([x, y]) + offset_unit
18
+ end
19
+
20
+ def top_right
21
+ top_left + width_unit
22
+ end
23
+
24
+ def bottom_right
25
+ top_left + width_unit + height_unit
26
+ end
27
+
28
+ def bottom_left
29
+ top_left + height_unit
30
+ end
31
+
32
+ def points
33
+ [
34
+ top_left,
35
+ top_right,
36
+ bottom_right,
37
+ bottom_left,
38
+ ]
39
+ end
40
+
41
+ def build
42
+ GridGenerator::BaseElement.new(points: points, colour: colour, opacity: opacity)
43
+ end
44
+ end
45
+ end
46
+ end