-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg_generator.py
172 lines (131 loc) · 5.65 KB
/
svg_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pickle
import svgwrite
from copy import deepcopy
from initialize import *
from piece import *
from misc import *
from bfs_solver import *
class Direction(Enum):
UP = 1
RIGHT = 2
DOWN = 3
LEFT = 4
class PartGeometryFactory:
def __init__(self):
self.base_geometry = dict()
self.base_geometry[Shape.ARROW_IN] = [(0, 2), (1, 1), (1, 2), (1.5, 2),
(1.5, 3), (1, 3), (1, 4), (0, 3)]
self.base_geometry[Shape.ARROW_OUT] = [(0, 2), (0.5, 2), (0.5, 1),
(2, 2.5), (0.5, 4), (0.5, 3),
(0, 3)]
self.base_geometry[Shape.CIRCLE] = [(0, 2), (0.5, 1.5), (1, 1.5), (1.5, 2),
(1.5, 3), (1, 3.5), (0.5, 3.5),
(0, 3)]
self.base_geometry[Shape.CROSS] = [(0, 2), (0.5, 2), (0.5, 1.5), (1, 1.5),
(1, 2), (1.5, 2), (1.5, 3), (1, 3),
(1, 3.5), (0.5, 3.5), (0.5, 3), (0, 3)]
@staticmethod
def switch_xy(geometry):
switched_geometry = list()
for (x, y) in geometry:
switched_geometry.append((y, x))
return switched_geometry
@staticmethod
def mirror_y(geometry):
mirrored_geometry = list()
for (x, y) in geometry:
mirrored_geometry.append((-x, y))
return mirrored_geometry
@staticmethod
def mirror_x(geometry):
mirrored_geometry = list()
for (x, y) in geometry:
mirrored_geometry.append((x, -y))
return mirrored_geometry
@staticmethod
def translate(geometry, translation):
translated_geometry = list()
(x_t, y_t) = translation
for (x, y) in geometry:
translated_geometry.append((x + x_t, y + y_t))
return translated_geometry
def __call__(self, shape: Shape, direction: Direction, orientation: Orientation):
geometry = deepcopy(self.base_geometry[shape])
if direction == Direction.LEFT:
if orientation == Orientation.IN:
return geometry
elif orientation == Orientation.OUT:
return self.mirror_y(geometry)
if direction == Direction.RIGHT:
if orientation == Orientation.OUT:
return self.translate(geometry, (5, 0))
elif orientation == Orientation.IN:
return self.translate(self.mirror_y(geometry), (5, 0))
switched_geometry = self.switch_xy(geometry)
if direction == Direction.UP:
if orientation == Orientation.IN:
return switched_geometry
elif orientation == Orientation.OUT:
return self.mirror_x(switched_geometry)
elif direction == Direction.DOWN:
if orientation == Orientation.OUT:
return self.translate(switched_geometry, (0, 5))
elif orientation == Orientation.IN:
return self.translate(self.mirror_x(switched_geometry), (0, 5))
class PieceGeometryFactory:
def __init__(self, scale=1):
self.scale = 1/5 * scale
def scale_geometry(self, geometry):
scaled_geometry = list()
for (x, y) in geometry:
scaled_geometry.append((x * self.scale, y * self.scale))
return scaled_geometry
@staticmethod
def reverse_geometry(geometry):
reversed_geometry = list()
for point in geometry:
reversed_geometry.insert(0, point)
return reversed_geometry
def __call__(self, piece):
pgf = PartGeometryFactory()
piece_geometry = list()
piece_geometry.append((0, 0))
left_part = piece.current_state.left
piece_geometry += pgf(left_part.shape, Direction.LEFT, left_part.orientation)
piece_geometry.append((0, 5))
down_part = piece.current_state.down
piece_geometry += pgf(down_part.shape, Direction.DOWN, down_part.orientation)
piece_geometry.append((5, 5))
right_part = piece.current_state.right
piece_geometry += self.reverse_geometry(pgf(right_part.shape, Direction.RIGHT, right_part.orientation))
piece_geometry.append((5, 0))
up_part = piece.current_state.up
piece_geometry += self.reverse_geometry(pgf(up_part.shape, Direction.UP, up_part.orientation))
return self.scale_geometry(piece_geometry)
def translate_piece_geometry(geometry, translation):
translated_geometry = list()
(x_t, y_t) = translation
for (x, y) in geometry:
translated_geometry.append((x + x_t, y + y_t))
return translated_geometry
def generate_svg_solution(solution, filename, scale=100):
"""
:param solution: Solution in form of Puzzle object.
:param filename: Filename of output SVG file.
:param scale: Piece side length in pixels.
:return:
"""
piece_geometry_factory = PieceGeometryFactory(scale)
mm = initialize_mapping_matrix()
dwg = svgwrite.Drawing(filename, (scale * 6, scale * 6), debug=True)
pieces_svg = dwg.add(dwg.g(id='original-pieces', stroke='black', fill='gray'), )
for i in range(16):
x = mm[i][0][0]
y = mm[i][1][0]
piece = solution.grid[x][y]
piece_geometry = piece_geometry_factory(piece)
# Made a bit of mess with coordinate systems: image coordinate system proved to have origin
# in upper left corner, while I thought it was in bottom left one. Thus (5 - y).
translated_pg = translate_piece_geometry(piece_geometry, (x * scale, (5 - y) * scale))
pieces_svg.add(dwg.polygon(translated_pg))
dwg.save()