1
+ from unittest import skip
1
2
from grid import Grid
2
3
from polar_cell import PolarCell
3
4
import math
@@ -52,7 +53,7 @@ def random_cell(self):
52
53
return random .choice (row )
53
54
54
55
def center_cell (self ):
55
- return self .grid [0 ][0 ]
56
+ return self .grid [0 ][0 ]
56
57
57
58
def to_png (self , cell_size = 30 , index = "0" ):
58
59
img_size = 2 * self .rows * cell_size
@@ -148,6 +149,98 @@ def to_svg(self, cell_size = 10):
148
149
dwg .add (dwg .circle (center = (center , center ), r = (self .rows * cell_size ), stroke = svgwrite .rgb (10 , 10 , 16 , '%' ), fill = 'none' ))
149
150
dwg .save ()
150
151
152
+
153
+ def to_svg_contour (self , cell_size = 10 , with_distance = False ):
154
+
155
+ img_size = 2 * self .rows * cell_size
156
+ margin = 10
157
+ center = img_size / 2 + margin
158
+ insert = ("" , "_distances" )[with_distance ]
159
+ if with_distance :
160
+ dwg = svgwrite .Drawing ('./exports/polarmaze_distance.svg' )
161
+ else :
162
+ dwg = svgwrite .Drawing ('./exports/polarmaze.svg' )
163
+
164
+ for cell in self .each_cell ():
165
+ if cell .row == 0 : continue
166
+
167
+ theta = 2 * math .pi / len (self .grid [cell .row ])
168
+
169
+ inner_radius = cell .row * cell_size
170
+ center_radius = inner_radius + cell_size / 2
171
+ outer_radius = inner_radius + cell_size
172
+
173
+ theta_ccw = cell .column * theta
174
+ theta_q = theta_ccw + theta / 4
175
+ theta_center = theta_ccw + (theta / 2 )
176
+ theta_s = theta_ccw + (3 * theta / 4 )
177
+ theta_cw = theta_ccw + theta
178
+
179
+ # The polar cell points
180
+ # o - origin
181
+ # abcd - edges
182
+ # vw -
183
+ #
184
+ # + a +
185
+ # dsoqc
186
+ # +tbr+
187
+ #
188
+
189
+ a_coord = polarToRect (center , inner_radius , theta_center )
190
+ o_coord = polarToRect (center , center_radius , theta_center )
191
+ b_coord = polarToRect (center , outer_radius , theta_center )
192
+
193
+ c_coord = polarToRect (center , center_radius , theta_ccw )
194
+ d_coord = polarToRect (center , center_radius , theta_cw )
195
+
196
+ q_coord = polarToRect (center , center_radius , theta_q )
197
+ r_coord = polarToRect (center , outer_radius , theta_q )
198
+ s_coord = polarToRect (center , center_radius , theta_s )
199
+ t_coord = polarToRect (center , outer_radius , theta_s )
200
+
201
+ if with_distance :
202
+ percent = (cell .distance % 200 ) / 2
203
+ color = svgwrite .rgb (percent , 10 , 10 , '%' )
204
+ if cell .distance % 600 > 200 :
205
+ color = svgwrite .rgb (10 , percent , 10 , '%' )
206
+ if cell .distance % 600 > 400 :
207
+ color = svgwrite .rgb (10 , 10 , percent , '%' )
208
+ dwg .add (dwg .text (cell .distance , insert = (o_coord .x , o_coord .y ), font_size = '5px' , fill = color ))
209
+
210
+ if has_corner (cell ):
211
+ if cell .linked (cell .inward ) and cell .linked (cell .ccw ):
212
+ addArc (dwg , (a_coord .x , a_coord .y ), (c_coord .x , c_coord .y ), cell_size / 1.4 , 'black' )
213
+ if len (cell .outward ) == 1 and cell .linked (cell .outward [0 ]) and cell .linked (cell .ccw ):
214
+ addArc (dwg , (c_coord .x , c_coord .y ), (b_coord .x , b_coord .y ), cell_size / 1.4 , 'black' )
215
+ if cell .linked (cell .inward ) and cell .linked (cell .cw ):
216
+ addArc (dwg , (d_coord .x , d_coord .y ), (a_coord .x , a_coord .y ), cell_size / 1.4 , 'black' )
217
+ if len (cell .outward ) == 1 and cell .linked (cell .outward [0 ]) and cell .linked (cell .cw ):
218
+ addArc (dwg , (b_coord .x , b_coord .y ), (d_coord .x , d_coord .y ), cell_size / 1.4 , 'black' )
219
+ else :
220
+ if cell .linked (cell .inward ):
221
+ if cell .distance == 1 :
222
+ dwg .add (dwg .line ((o_coord .x , o_coord .y ), (center , center ), stroke = "black" ))
223
+ else :
224
+ dwg .add (dwg .line ((o_coord .x , o_coord .y ), (a_coord .x , a_coord .y ), stroke = "black" ))
225
+ if cell .linked (cell .cw ):
226
+ addArc (dwg , (d_coord .x , d_coord .y ), (o_coord .x , o_coord .y ), center_radius , 'black' )
227
+ if cell .linked (cell .ccw ):
228
+ addArc (dwg , (o_coord .x , o_coord .y ), (c_coord .x , c_coord .y ), center_radius , 'black' )
229
+ if len (cell .outward ) == 1 :
230
+ if cell .outward [0 ].linked (cell ):
231
+ dwg .add (dwg .line ((o_coord .x , o_coord .y ), (b_coord .x , b_coord .y ), stroke = "black" ))
232
+ if len (cell .outward ) == 2 :
233
+ if cell .outward [0 ].linked (cell ):
234
+ addArc (dwg , (o_coord .x , o_coord .y ), (q_coord .x , q_coord .y ), center_radius , 'black' )
235
+ dwg .add (dwg .line ((q_coord .x , q_coord .y ), (r_coord .x , r_coord .y ), stroke = "black" ))
236
+ if cell .outward [1 ].linked (cell ):
237
+ addArc (dwg , (s_coord .x , s_coord .y ), (o_coord .x , o_coord .y ), center_radius , 'black' )
238
+ dwg .add (dwg .line ((s_coord .x , s_coord .y ), (t_coord .x , t_coord .y ), stroke = "black" ))
239
+
240
+
241
+ # dwg.add(dwg.circle(center=(center, center), r=(self.rows * cell_size), stroke=svgwrite.rgb(10, 10, 16, '%'), fill='none'))
242
+ dwg .save ()
243
+
151
244
def addArc (dwg , p0 , p1 , radius , color ):
152
245
""" Adds an arc that bulges to the right as it moves from p0 to p1 """
153
246
# https://stackoverflow.com/questions/25019441/arc-pie-cut-in-svgwrite
@@ -172,4 +265,15 @@ def showCell(dwg, grid, cell, cell_size, center):
172
265
theta = 2 * math .pi / len (grid [cell .row ])
173
266
angle = (cell .column + .5 ) * theta
174
267
coords = polarToRect (center , radius , angle )
175
- dwg .add (dwg .circle (center = (coords .x , coords .y ), r = (cell_size * .25 ), stroke = svgwrite .rgb (10 , 10 , 16 , '%' ), fill = 'red' ))
268
+ dwg .add (dwg .circle (center = (coords .x , coords .y ), r = (cell_size * .25 ), stroke = svgwrite .rgb (10 , 10 , 16 , '%' ), fill = 'red' ))
269
+
270
+ def has_corner (cell ):
271
+ if len (cell .get_links ()) != 2 :
272
+ return False
273
+ if len (cell .outward ) == 2 :
274
+ return False
275
+ if cell .linked (cell .inward ) and len (cell .outward ) == 1 and cell .linked (cell .outward [0 ]):
276
+ return False
277
+ if cell .linked (cell .ccw ) and cell .linked (cell .cw ):
278
+ return False
279
+ return True
0 commit comments