grid_generator 0.3.0 → 0.3.2

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: d75cdb06a79ebb644afccc577353a18b2a7f999251ffffd6f1f7617b08c23745
4
- data.tar.gz: fb3fc73597223fed891b6857250e217f9c4ca057e1795574b2ae1a9fd2ce1c41
3
+ metadata.gz: bac6da73c0f3f0fae6985d7c6e9b2f91f75ff82faaaf6d4a5299af71e0980e3c
4
+ data.tar.gz: 53848371057b7cbb7743049bc992cdee7eb17fed5e26412fb3efe101710cf688
5
5
  SHA512:
6
- metadata.gz: 06f2a4c827b2710abc6a379f6a45360a7caabafa4b0905f466e975968c0a622bb1f87296d7409b6225dabc7f807f126b4c93633af4b8a449830eb27703a5aee7
7
- data.tar.gz: 1cb5b6a79655216376731edcd9724ceb9dd9cb5e6236df9cab39c8e048754a0aca33a17413a71d87aeb8985c48a74ad0500ac8f1035b2c7784a948db9b3f3976
6
+ metadata.gz: b75fad2b601e6b0b027961e53b0562bd5a916309c12dd161a7471e1805746bf694e9908d3433c3e00bddef4e5e7fb1ad190c2d8063ac88ffa061a8641325d45a
7
+ data.tar.gz: 0d4ee932026ea66b66ef3121bdf422bc56f1003c2acb4f4e461014eb20b80b1114c106965d0565a202dd767c9ca3eaf922909f1e0fc24ccd0220bb953afd8b88
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grid_generator (0.3.0)
4
+ grid_generator (0.3.2)
5
5
  matrix (~> 0.4.2)
6
6
 
7
7
  GEM
@@ -1,5 +1,10 @@
1
1
  module GridGenerator
2
2
  class BaseElement
3
+ COLOURS = {
4
+ fill: "#d0d0d0",
5
+ stroke: "#404040"
6
+ }
7
+
3
8
  def initialize(points:, colour: , opacity: 1)
4
9
  @points = points
5
10
  @colour = colour
@@ -26,5 +31,9 @@ module GridGenerator
26
31
  "opacity" => opacity
27
32
  }
28
33
  end
34
+
35
+ def to_svg
36
+ "<polygon points=\"#{points_string}\" style=\"fill:#{colour};stroke:#{COLOURS[:stroke]};stroke-width:1;opacity:#{opacity}\" />"
37
+ end
29
38
  end
30
39
  end
@@ -1,4 +1,6 @@
1
1
  require 'matrix'
2
+ require_relative '../svg/polygon'
3
+ require_relative '../svg/style'
2
4
  require_relative '../line'
3
5
  require_relative '../face_parser'
4
6
  require_relative 'facing_square_factory'
@@ -6,6 +8,11 @@ require_relative 'facing_square_factory'
6
8
  module GridGenerator
7
9
  module Cubic
8
10
  class BorderedGrid
11
+ COLOURS = {
12
+ fill: "#d0d0d0",
13
+ stroke: "#404040"
14
+ }
15
+
9
16
  def initialize(x:, y:, units:, squares: )
10
17
  @x, @y = x, y
11
18
  @units = units
@@ -123,18 +130,23 @@ module GridGenerator
123
130
  Matrix.column_vector([ x, max_y ])
124
131
  ]
125
132
  end
126
-
127
- def points_string
128
- points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
133
+
134
+ def base_shape_style
135
+ GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
129
136
  end
130
137
 
131
- def as_json
132
- {
133
- "points_string" => points_string,
134
- "rows" => rows,
135
- "columns" => columns,
136
- "element_shapes" => element_shapes.map(&:as_json)
137
- }
138
+ def base_shape
139
+ GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
140
+ end
141
+
142
+ def to_svg
143
+ output = base_shape.to_svg
144
+
145
+ rows.each { |row| output += row.to_svg }
146
+ columns.each { |col| output += col.to_svg }
147
+ element_shapes.each { |shape| output += shape.to_svg }
148
+
149
+ output
138
150
  end
139
151
  end
140
152
  end
@@ -1,4 +1,6 @@
1
1
  require 'matrix'
2
+ require_relative '../svg/polygon'
3
+ require_relative '../svg/style'
2
4
  require_relative '../face_parser'
3
5
  require_relative '../line'
4
6
  require_relative 'facing_square_factory'
@@ -6,6 +8,11 @@ require_relative 'facing_square_factory'
6
8
  module GridGenerator
7
9
  module Cubic
8
10
  class FacingGrid
11
+ COLOURS = {
12
+ fill: "#d0d0d0",
13
+ stroke: "#404040"
14
+ }
15
+
9
16
  def initialize(x:, y:, units:, squares: )
10
17
  @x, @y = x, y
11
18
  @units = units
@@ -103,18 +110,23 @@ module GridGenerator
103
110
  Matrix.column_vector([ x, max_y ])
104
111
  ]
105
112
  end
106
-
107
- def points_string
108
- points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
113
+
114
+ def base_shape_style
115
+ GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
109
116
  end
110
117
 
111
- def as_json
112
- {
113
- "points_string" => points_string,
114
- "rows" => rows,
115
- "columns" => columns,
116
- "element_shapes" => element_shapes.map(&:as_json)
117
- }
118
+ def base_shape
119
+ GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
120
+ end
121
+
122
+ def to_svg
123
+ output = base_shape.to_svg
124
+
125
+ rows.each { |row| output += row.to_svg }
126
+ columns.each { |col| output += col.to_svg }
127
+ element_shapes.each { |shape| output += shape.to_svg }
128
+
129
+ output
118
130
  end
119
131
  end
120
132
  end
@@ -4,6 +4,11 @@ require_relative '../line'
4
4
  module GridGenerator
5
5
  module Cubic
6
6
  class Grid
7
+ COLOURS = {
8
+ fill: "#d0d0d0",
9
+ stroke: "#404040"
10
+ }
11
+
7
12
  def initialize(x:, y:, units: , squares: )
8
13
  @x, @y = x, y
9
14
  @units = units
@@ -148,18 +153,23 @@ module GridGenerator
148
153
  bottom_left
149
154
  ]
150
155
  end
151
-
152
- def points_string
153
- points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
156
+
157
+ def base_shape_style
158
+ GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
154
159
  end
155
160
 
156
- def as_json
157
- {
158
- "points_string" => points_string,
159
- "rows" => rows,
160
- "columns" => columns,
161
- "element_shapes" => element_shapes.map(&:as_json)
162
- }
161
+ def base_shape
162
+ GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
163
+ end
164
+
165
+ def to_svg
166
+ output = base_shape.to_svg
167
+
168
+ rows.each { |row| output += row.to_svg }
169
+ columns.each { |col| output += col.to_svg }
170
+ element_shapes.each { |shape| output += shape.to_svg }
171
+
172
+ output
163
173
  end
164
174
  end
165
175
  end
@@ -29,6 +29,13 @@ module GridGenerator
29
29
  "right" => right.as_json
30
30
  }
31
31
  end
32
+
33
+ def to_svg
34
+ output = top.to_svg
35
+ output += front.to_svg
36
+ output += right.to_svg
37
+ output
38
+ end
32
39
  end
33
40
  end
34
41
  end
@@ -1,5 +1,10 @@
1
1
  module GridGenerator
2
2
  class Line
3
+ COLOURS = {
4
+ fill: "#d0d0d0",
5
+ stroke: "#404040"
6
+ }
7
+
3
8
  def initialize(a:, b:)
4
9
  @a, @b = a, b
5
10
  end
@@ -37,6 +42,10 @@ module GridGenerator
37
42
  def y2
38
43
  b[1,0]
39
44
  end
45
+
46
+ def to_svg
47
+ "<line x1=\"#{x1}\" y1=\"#{y1}\" x2=\"#{x2}\" y2=\"#{y2}\" style=\"stroke:#{COLOURS[:stroke]};stroke-width:1\" />"
48
+ end
40
49
  end
41
50
  end
42
51
 
@@ -1,4 +1,6 @@
1
1
  require 'matrix'
2
+ require_relative '../svg/polygon'
3
+ require_relative '../svg/style'
2
4
  require_relative '../rotator'
3
5
  require_relative '../helper'
4
6
  require_relative '../line'
@@ -7,6 +9,10 @@ require_relative 'face_element_factory'
7
9
  module GridGenerator
8
10
  module Megaminx
9
11
  class FaceProjection
12
+ COLOURS = {
13
+ fill: "#d0d0d0",
14
+ stroke: "#404040"
15
+ }
10
16
  # units 30 - pentagon 90 - megaminx - 150
11
17
  # units * 5
12
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)
@@ -145,13 +151,13 @@ module GridGenerator
145
151
  end
146
152
 
147
153
  # for svg
148
- def decagon_points_string
149
- decagon_points.map { |p| offset_rotator.rotate(p) + offset }.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
154
+ def decagon_points_transformed
155
+ decagon_points.map { |p| offset_rotator.rotate(p) + offset }
150
156
  end
151
157
 
152
158
  # for svg
153
- def pentagon_points_string
154
- pentagon_points.map { |p| offset_rotator.rotate(p) + offset }.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
159
+ def pentagon_points_transformed
160
+ pentagon_points.map { |p| offset_rotator.rotate(p) + offset }
155
161
  end
156
162
 
157
163
  # for svg
@@ -205,6 +211,41 @@ module GridGenerator
205
211
  end.compact
206
212
  end
207
213
  end
214
+
215
+ def decagon_shape_style
216
+ GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
217
+ end
218
+
219
+ def decagon_shape
220
+ GridGenerator::Svg::Polygon.new(points: decagon_points_transformed, style: decagon_shape_style)
221
+ end
222
+
223
+ def pentagon_shape_style
224
+ GridGenerator::Svg::Style.new(fill: 'none', stroke: COLOURS[:stroke])
225
+ end
226
+
227
+ def pentagon_shape
228
+ GridGenerator::Svg::Polygon.new(points: pentagon_points_transformed, style: pentagon_shape_style)
229
+ end
230
+
231
+ def to_svg
232
+ output = decagon_shape.to_svg
233
+ output += pentagon_shape.to_svg
234
+
235
+ connecting_lines.each { |line| output += line.to_svg }
236
+
237
+ front_face_lines.each { |line| output += line.to_svg }
238
+ outside_face_lines.each do |face|
239
+ face.each { |line| output += line.to_svg }
240
+ end
241
+
242
+ front_face_element_shapes.each { |shape| output += shape.to_svg }
243
+ outside_face_element_shapes.each do |face|
244
+ face.each { |shape| output += shape.to_svg }
245
+ end
246
+
247
+ output
248
+ end
208
249
  end
209
250
  end
210
251
  end
@@ -1,7 +1,9 @@
1
- require_relative 'triangle_factory'
1
+ require_relative '../svg/polygon'
2
+ require_relative '../svg/style'
2
3
  require_relative '../rotator'
3
4
  require_relative '../scaler'
4
5
  require_relative '../line'
6
+ require_relative 'triangle_factory'
5
7
 
6
8
  module GridGenerator
7
9
  module Pyraminx
@@ -9,6 +11,11 @@ module GridGenerator
9
11
  # * * *
10
12
  # * * * * *
11
13
  class Face
14
+ COLOURS = {
15
+ fill: "#d0d0d0",
16
+ stroke: "#404040"
17
+ }
18
+
12
19
  def initialize(x:, y:, units:, elements:, vertical_scale: 1, rotation_angle: 0)
13
20
  @x, @y = x, y
14
21
  @units = units
@@ -169,10 +176,6 @@ module GridGenerator
169
176
  end
170
177
  end
171
178
 
172
- def points_string
173
- points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
174
- end
175
-
176
179
  def element_shapes
177
180
  elements.map.each_with_index do |row, row_num|
178
181
  row.map.each_with_index do |col, col_num|
@@ -190,6 +193,25 @@ module GridGenerator
190
193
  end
191
194
  end.flatten.compact
192
195
  end
196
+
197
+ def base_shape_style
198
+ GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
199
+ end
200
+
201
+ def base_shape
202
+ GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
203
+ end
204
+
205
+ def to_svg
206
+ output = base_shape.to_svg
207
+
208
+ vertical_lines.each { |line| output += line.to_svg }
209
+ diagonal_up_lines.each { |line| output += line.to_svg }
210
+ diagonal_down_lines.each { |line| output += line.to_svg }
211
+ element_shapes.each { |shape| output += shape.to_svg }
212
+
213
+ output
214
+ end
193
215
  end
194
216
  end
195
217
  end
@@ -9,12 +9,12 @@ module GridGenerator
9
9
  GridGenerator::Skewb::LeftElementFactory
10
10
  end
11
11
 
12
- def border_points
12
+ def points
13
13
  [
14
- [ x, y ],
15
- [ x + side_size*2*units, y + side_size*units ],
16
- [ x + side_size*2*units, y + side_size*3*units ],
17
- [ x, y + side_size*2*units ]
14
+ Matrix.column_vector([ x, y ]),
15
+ Matrix.column_vector([ x + side_size*2*units, y + side_size*units ]),
16
+ Matrix.column_vector([ x + side_size*2*units, y + side_size*3*units ]),
17
+ Matrix.column_vector([ x, y + side_size*2*units ])
18
18
  ]
19
19
  end
20
20
 
@@ -10,12 +10,12 @@ module GridGenerator
10
10
  GridGenerator::Skewb::RightElementFactory
11
11
  end
12
12
 
13
- def border_points
13
+ def points
14
14
  [
15
- [ x, y + side_size*units ],
16
- [ x + side_size*2*units, y ],
17
- [ x + side_size*2*units, y + side_size*2*units ],
18
- [ x, y + side_size*3*units ]
15
+ Matrix.column_vector([ x, y + side_size*units ]),
16
+ Matrix.column_vector([ x + side_size*2*units, y ]),
17
+ Matrix.column_vector([ x + side_size*2*units, y + side_size*2*units ]),
18
+ Matrix.column_vector([ x, y + side_size*3*units ])
19
19
  ]
20
20
  end
21
21
 
@@ -3,6 +3,11 @@ require_relative '../face_parser'
3
3
  module GridGenerator
4
4
  module Skewb
5
5
  class SkewbGrid
6
+ COLOURS = {
7
+ fill: "#d0d0d0",
8
+ stroke: "#404040"
9
+ }
10
+
6
11
  def initialize(x:, y:, units: , elements: )
7
12
  @x, @y = x, y
8
13
  @units = units
@@ -39,10 +44,6 @@ module GridGenerator
39
44
  end
40
45
  end
41
46
 
42
- def border_points_string
43
- border_points.map { |x| x.join(',') }.join(' ')
44
- end
45
-
46
47
  def element_shapes
47
48
  elements.each_with_index.map do |row, row_num|
48
49
  row.each_with_index.map do |col, col_num|
@@ -51,11 +52,22 @@ module GridGenerator
51
52
  end.flatten.compact
52
53
  end
53
54
 
54
- def as_json
55
- {
56
- "border_points_string" => border_points_string,
57
- "element_shapes" => element_shapes.map(&:as_json)
58
- }
55
+ def base_shape_style
56
+ GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
57
+ end
58
+
59
+ def base_shape
60
+ GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
61
+ end
62
+
63
+ def to_svg
64
+ output = base_shape.to_svg
65
+
66
+ rows.each { |row| output += row.to_svg }
67
+ columns.each { |col| output += col.to_svg }
68
+ element_shapes.each { |element| output += element.to_svg if element }
69
+
70
+ output
59
71
  end
60
72
  end
61
73
  end
@@ -10,12 +10,12 @@ module GridGenerator
10
10
  GridGenerator::Skewb::TopElementFactory
11
11
  end
12
12
 
13
- def border_points
13
+ def points
14
14
  [
15
- [ x + side_size*2*units, y ],
16
- [ x + side_size*4*units, y + side_size*units ],
17
- [ x + side_size*2*units, y + side_size*2*units ],
18
- [ x, y + side_size*units ]
15
+ Matrix.column_vector([ x + side_size*2*units, y ]),
16
+ Matrix.column_vector([ x + side_size*4*units, y + side_size*units ]),
17
+ Matrix.column_vector([ x + side_size*2*units, y + side_size*2*units ]),
18
+ Matrix.column_vector([ x, y + side_size*units ])
19
19
  ]
20
20
  end
21
21
 
@@ -5,6 +5,11 @@ require_relative 'element_factory'
5
5
  module GridGenerator
6
6
  module SquareOne
7
7
  class Face
8
+ COLOURS = {
9
+ fill: "#d0d0d0",
10
+ stroke: "#404040"
11
+ }
12
+
8
13
  def initialize(x:, y: , units: , elements:, axis_direction: :forward)
9
14
  @x, @y = x, y
10
15
  @units = units
@@ -88,6 +93,20 @@ module GridGenerator
88
93
  "element_shapes" => element_shapes.map(&:as_json)
89
94
  }
90
95
  end
96
+
97
+ def to_svg
98
+ output = ""
99
+ element_shapes.each do |element|
100
+ if element.opacity == 0.4
101
+ output += "<polygon points=\"#{ element.points_string }\" style=\"fill:#{ COLOURS[:fill] };stroke:#{ COLOURS[:stroke] };stroke-width:1;opacity:1;\" />"
102
+ end
103
+ output += "<polygon points=\"#{ element.points_string }\" style=\"fill:#{ element.colour };stroke:#{ COLOURS[:stroke] };stroke-width:1;opacity:#{ element.opacity };\" />"
104
+ end
105
+
106
+ output += "<line x1=\"#{ axis.x1 }\" y1=\"#{ axis.y1 }\" x2=\"#{ axis.x2 }\" y2=\"#{ axis.y2 }\" style=\"stroke:#{ COLOURS[:stroke] };stroke-width:5\" />"
107
+
108
+ output
109
+ end
91
110
  end
92
111
  end
93
112
  end
@@ -0,0 +1,21 @@
1
+ module GridGenerator
2
+ module Svg
3
+ class Polygon
4
+ def initialize(points: , style:)
5
+ @points = case points
6
+ when Array
7
+ points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
8
+ else
9
+ points
10
+ end
11
+ @style = style
12
+ end
13
+
14
+ attr_reader :points, :style
15
+
16
+ def to_svg
17
+ "<polygon points=\"#{points}\" style=\"#{style.to_s}\" />"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module GridGenerator
2
+ module Svg
3
+ class Style
4
+ def initialize(fill: , stroke: , stroke_width: 1, opacity: 1)
5
+ @fill = fill
6
+ @stroke = stroke
7
+ @stroke_width = stroke_width
8
+ @opacity = opacity
9
+ end
10
+
11
+ attr_reader :fill, :stroke, :stroke_width, :opacity
12
+
13
+ def to_s
14
+ "fill:#{fill};stroke:#{stroke};stroke-width:#{stroke_width};opacity:#{opacity};"
15
+ end
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.3.0"
4
+ VERSION = "0.3.2"
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.3.0
4
+ version: 0.3.2
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-22 00:00:00.000000000 Z
11
+ date: 2023-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: matrix
@@ -70,6 +70,8 @@ files:
70
70
  - lib/grid_generator/square_one/element_factory.rb
71
71
  - lib/grid_generator/square_one/face.rb
72
72
  - lib/grid_generator/square_one_face_parser.rb
73
+ - lib/grid_generator/svg/polygon.rb
74
+ - lib/grid_generator/svg/style.rb
73
75
  - lib/grid_generator/version.rb
74
76
  - sig/grid_generator.rbs
75
77
  homepage: https://github.com/mrlhumphreys/grid_generator