def render(maze, filename, tweaker=tweak, title="Prim's Algorithm"): """render a maze This is an entry point to this script. If the script is run from the command line, a weave maze will be created and carved using Prim's algorithm. maze = a rectangular or weave maze to be rendered filename = filename or pathname for output tweaker = the tweaking routine (default tweak). This function takes three parameters: a Figure object, an Axe objects, and a title """ # create a subplot array fig, ax = plt.subplots(1, 1) tweaker(fig, ax, title) # generate the plots print("Plotting maze...") layout = Color_Layout(maze, plt, figure=[fig, ax]) layout.palette[0] = "yellow" layout.palette[1] = "brown" for cell in maze.each(): layout.color[cell] = 1 if "underCell" in cell.kwargs else 0 layout.draw_grid() print("Saved to " + filename) layout.render(filename) return layout
def render_plot(grid, masked, pathname): """render the maze using matplotlib""" import matplotlib.pyplot as plt from layout_plot_color import Color_Layout layout = Color_Layout(grid, plt, title='Masked Grid') layout.set_palette_color(1, 'red') for cell in grid.each(): if not masked.is_enabled(cell): layout.set_color(cell, 1) layout.ax.set(aspect=1) plt.axis('off') layout.draw_grid() plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) layout.render(pathname, tight=True)
def render_plot(m, n, algorithm, bias): """render the maze using matplotlib""" import matplotlib.pyplot as plt from layout_plot_color import Color_Layout from norms import distances fig, axs = plt.subplots(2, 3) x0, y0 = int(m / 2), int(n / 2) basename = algorithm for i in range(2): for j in range(3): grid = make_grid(m, n) center = grid[x0, y0] assert center, "Undefined grid center cell" basename = generate_maze(algorithm, grid, bias) ax = axs[i, j] layout = Color_Layout(grid, plt, figure=[fig, ax]) # We use the distance from a middle cell as the # palette index norms = distances(center) furthest = norms.furthest_from_root() maxdist = norms[furthest] for dist in range(maxdist + 1): layout.set_palette_color(dist, define_color(dist, maxdist)) for cell in grid.each(): dist = norms[cell] if dist is not None: layout.set_color(cell, dist) ax.set(aspect=1) ax.axis('off') layout.draw_grid() fig.suptitle(basename) filename = "demos/" + basename + "-array.png" print("Saved to " + filename) plt.subplots_adjust(hspace=.001, wspace=.001) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) fig.savefig(filename, bbox_inches='tight', pad_inched=0.0)
def render(maze, categories, colors, filename, title): """plot the maze""" print("Rendering -- this may take some time!") print(" %d cells in maze" % len(maze)) mpl.rcParams['lines.linewidth'] = 0.5 fig, ax = plt.subplots(figsize=(12, 3)) ax.set_axis_off() layout = Color_Layout(maze, plt, figure=[fig, ax], title=title) for category in colors: layout.set_palette_color(category, colors[category]) for category in categories: if category in colors: for cell in categories[category]: layout.set_color(cell, category) for cell in maze.each(): if "underCell" in cell.kwargs: layout.set_color(cell, "under") layout.draw_grid() layout.fig.savefig(filename, bbox_inches='tight', pad_inches=0.0, dpi=300)
def render(mazes, filename, tweaker=tweak): """render a set of mazes This is the main entry point to this script. If the script is run from the command line, eight rectangular weave grids will be created, and perfect mazes will be created from them using various growing tree algorithms. mazes = a (2,4) matrix of mazes. filename = filename or pathname for output. tweaker = the tweaking routine (default tweak). This function takes two parameters: a Figure object and a 2x4 matrix of Axes objects. """ mpl.rcParams['lines.linewidth'] = 0.5 # create a subplot array fig, axs = plt.subplots(2, 4) tweaker(fig, axs) # generate the plots for i in range(2): for j in range(4): print(" plotting maze %d..." % (4 * i + j + 1)) maze = mazes[i][j] ax = axs[i][j] layout = Color_Layout(maze, plt, figure=[fig, ax]) layout.palette[0] = "yellow" layout.palette[1] = "green" for cell in maze.each(): layout.color[cell] = 1 if "underCell" in cell.kwargs \ else 0 layout.draw_grid() print("Saved to " + filename) plt.subplots_adjust(hspace=.001, wspace=.001) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) fig.savefig(filename, bbox_inches='tight', pad_inched=0.0, dpi=300) plt.show()
def main(m, n, inset): """create an inset maze""" print("create grid: Rectangular_Grid(%d, %d, inset=%f)" % (m, n, inset)) grid = Rectangular_Grid(m, n, inset=inset) print("Create perfect maze using randomized DFS...") DFS.on(grid) layout = Color_Layout(grid, plt, title="Inset Maze Demonstration") print("Creating color palette...") layout.palette[0] = 'violet' layout.palette[1] = 'powderblue' layout.palette[2] = 'red' layout.palette[3] = 'green' for i in range(m): for j in range(n): layout.color[grid[i, j]] = (i + j) % 2 layout.color[grid[0, 0]] = 2 layout.color[grid[m - 1, n - 1]] = 3 print("Plotting...") layout.draw_grid() layout.render("demos/inset_demo.png")
def render(mazes, filename, tweaker=tweak, titles=titles3): """render a set of mazes This is the main entry point to this script. If the script is run from the command line, three rectangular weave grids will be created, and perfect mazes will be created from them using depth-first search, hunt and kill, and first-entry Aldous/Broder. For each given maze, a subplots is produced: a) a plot of the given maze b) a plot of the maze after partial braiding c) a plot of the maze after braiding is complete filename = filename or pathname for output tweaker = the tweaking routine (default tweak). This function takes three parameters: a Figure object, an array of Axes objects, and a set of maze titles""" # create a subplot array cols = len(mazes) fig, axs = plt.subplots(1, len(mazes)) tweaker(fig, axs, titles) # generate the plots for j in range(cols): print(" plotting maze %d..." % (j + 1)) maze = mazes[j] ax = axs[j] layout = Color_Layout(maze, plt, figure=[fig, ax]) layout.palette[0] = "yellow" layout.palette[1] = "green" for cell in maze.each(): layout.color[cell] = 1 if "underCell" in cell.kwargs else 0 layout.draw_grid() print("Saved to " + filename) plt.subplots_adjust(hspace=.001, wspace=.001) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) fig.savefig(filename, bbox_inches='tight', pad_inched=0.0)
def render(maze, filename, title, debug=False): """render a mazes This is the main entry point to this script. If the script is run from the command line, the maze will be created using recursive division algorithm. filename = filename or pathname for output title = a title for the printout """ # generate the plots print("Plotting...\n%s" % title) layout = Color_Layout(maze, plt, title=title) layout.ax.set(aspect=1) layout.ax.axis('off') layout.palette[0] = "yellow" layout.palette[1] = "brown" for cell in maze.each(): layout.color[cell] = 1 if "underCell" in cell.kwargs else 0 layout.draw_grid() layout.render(filename) if debug: layout.plt.show()
def render(maze, filename, p=0.3, tweaker=tweak, turns=None): """render a set of mazes This is the main entry point to this script. If the script is run from the command line, a rectangular grid will be created and a perfect mazes will be created from it using the first-entry Aldous/Broder algorithm. Three subplots are produced: a) a plot of the given maze b) a plot of the maze after single-pass braid-removal by twisting with bias 30% c) a plot of the maze after a complete straightening pass maze = a Grid object with a maze to braid by straightening filename = filename or pathname for output p = the bias to be used for partial braiding (default 0.3) tweaker = the tweaking routine (default tweak). This function takes three parameters: the number of mazes, a Figure object and array of Axes objects turns = a dictionary of turns. The entries have the form turns[direction] = [direction1, direction2, ...] If turns is None, the default turning dictionary is used. """ from layout_plot_color import Color_Layout # create a subplot array fig, axs = plt.subplots(1, 3) tweaker(fig, axs) # generate the plots print("Plotting...") print(" column 0 - given maze...") ax = axs[0] layout = Color_Layout(maze, plt, figure=[fig, ax]) layout.draw_grid() print(" column 1 - first pass, bias p=%f..." % (p * 100)) n, _, k, q = Braiding.twister(maze, bias=p, turns=turns) print(" %d dead ends, %d removed (q=%f)" % (n, k, q * 100)) ax = axs[1] layout = Color_Layout(maze, plt, figure=[fig, ax]) layout.draw_grid() print(" column 2 - first pass, full twisting...") n, _, k, q = Braiding.twister(maze) print(" %d dead ends, %d removed (q=%f)" % (n, k, q * 100)) ax = axs[2] layout = Color_Layout(maze, plt, figure=[fig, ax]) layout.draw_grid() print("Saved to " + filename) plt.subplots_adjust(hspace=.001, wspace=.001) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) fig.savefig(filename, bbox_inches='tight', pad_inched=0.0)