Skip to content

Commit 12693b5

Browse files
committed
curvy lines for polar maze
1 parent 9a778cb commit 12693b5

11 files changed

+120
-20
lines changed

Diff for: .DS_Store

0 Bytes
Binary file not shown.

Diff for: __pycache__/cell.cpython-310.pyc

286 Bytes
Binary file not shown.

Diff for: __pycache__/grid.cpython-310.pyc

-14 Bytes
Binary file not shown.

Diff for: __pycache__/polar_cell.cpython-310.pyc

0 Bytes
Binary file not shown.

Diff for: __pycache__/polar_grid.cpython-310.pyc

2.23 KB
Binary file not shown.

Diff for: __pycache__/recursive_backtracker.cpython-310.pyc

-117 Bytes
Binary file not shown.

Diff for: cell.py

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Cell:
55
def __init__(self, row, column):
66
self.row, self.column = row, column
77
self.links = {}
8+
self.distance = 0
89

910
def link(self, cell, bidi=True):
1011
self.links[cell] = True
@@ -22,6 +23,9 @@ def show_links(self):
2223
def get_links(self):
2324
return self.links.keys()
2425

26+
def get_links_full(self):
27+
return self.links
28+
2529
def linked(self, cell):
2630
if cell in self.links:
2731
return True
@@ -50,3 +54,6 @@ def distances(self):
5054

5155
frontier = new_frontier
5256
return distances
57+
58+
def set_distance(self, distance):
59+
self.distance = distance

Diff for: circle_maze.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from polar_grid import PolarGrid
22
from recursive_backtracker import RecursiveBacktrackerPolar
33

4-
grid = PolarGrid(10)
4+
grid = PolarGrid(22)
55
RecursiveBacktrackerPolar.mutate(grid)
6-
grid.to_svg()
6+
grid.to_svg_contour()
7+
grid.to_svg_contour(10, True)

Diff for: grid.py

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def __init__(self, rows, columns):
99
self.grid = self.prepare_grid()
1010
self.configure_cells()
1111
self.end = []
12-
self.path = []
1312

1413
def prepare_grid(self):
1514
grid_array = []

Diff for: polar_grid.py

+106-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest import skip
12
from grid import Grid
23
from polar_cell import PolarCell
34
import math
@@ -52,7 +53,7 @@ def random_cell(self):
5253
return random.choice(row)
5354

5455
def center_cell(self):
55-
return self.grid[0][0]
56+
return self.grid[0][0]
5657

5758
def to_png(self, cell_size=30, index = "0"):
5859
img_size = 2 * self.rows * cell_size
@@ -148,6 +149,98 @@ def to_svg(self, cell_size = 10):
148149
dwg.add(dwg.circle(center=(center, center), r=(self.rows * cell_size), stroke=svgwrite.rgb(10, 10, 16, '%'), fill='none'))
149150
dwg.save()
150151

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+
151244
def addArc(dwg, p0, p1, radius, color):
152245
""" Adds an arc that bulges to the right as it moves from p0 to p1 """
153246
# https://stackoverflow.com/questions/25019441/arc-pie-cut-in-svgwrite
@@ -172,4 +265,15 @@ def showCell(dwg, grid, cell, cell_size, center):
172265
theta = 2 * math.pi / len(grid[cell.row])
173266
angle = (cell.column + .5) * theta
174267
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

Diff for: recursive_backtracker.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,18 @@ def mutate(grid, animation = False):
2626
MakeAnimation(filenames, 'recursive_backtracker.gif')
2727

2828
class RecursiveBacktrackerPolar:
29-
def mutate(grid, animation = False):
30-
frame = 0
31-
found_end = False
32-
filenames = []
29+
def mutate(grid):
3330
stack = [grid.center_cell()]
3431

3532
while len(stack) > 0:
3633
current = stack[-1]
34+
next_distance = current.distance + 1
3735
neighbors = [cell for cell in current.neighbors() if len(cell.get_links()) == 0]
38-
if not found_end:
39-
grid.path.append(current)
4036
if len(neighbors) == 0:
41-
if not found_end:
42-
grid.end = current
43-
found_end = True
4437
stack.pop()
4538
else:
4639
neighbor = random.choice(neighbors)
4740
current.link(neighbor)
41+
neighbor.link(current)
42+
neighbor.set_distance(next_distance)
4843
stack.append(neighbor)
49-
if animation:
50-
frame += 1
51-
grid.to_png(20, str(frame))
52-
filenames.append("./exports/maze"+str(frame)+".png")
53-
if animation:
54-
MakeAnimation(filenames, 'recursive_backtracker.gif')

0 commit comments

Comments
 (0)