How to adjust wall / path parameters for a 2D maze using PIL

56 Views Asked by At

I'm trying to randomly generate a 2D maze for a line following robot. Using the below code, I've managed to generate the maze, and for simplicity, although not sure this is the best option, I've inverted the colours so the path is black and the walls are white. This produces a reasonably desirable result as seen below:

maze

However I'm struggling to achieve the following:

  • The code "should" be accounting for dimensions, so that eventually it can be printed in A4 sheets and taped together. This means when printed, the path must be 19mm and remain as such when the overall maze dimensions is scaled. I'm currently not convinced this is working as I had hoped.

  • Secondly, the internal width between the walls must always be at least 160mm, to accommodate the max width of the robot. Identifying a solution for this has stumped me! I've tried creating a maze with a path width of 160mm with a white background, then tried to create and overlay an identical maze but with a 19mm wide path. I never get this to work.

  • Finally I need a way of the code setting the start and end position. Ideally with a circle one end and a empty square the other. Again my attempts to draw shapes on the existing maze have not gone well.

trying to produce something similar to:

maze_concept

The code I have is:

 import random
from PIL import Image, ImageDraw

# Maze dimensions 
maze_width = 840
maze_height = 1188
path_width = 19

# Maze dimensions in cells (adjust for the path width)
num_cols = maze_width // path_width
num_rows = maze_height // path_width

# Create a grid for the maze
maze = [[0] * num_cols for _ in range(num_rows)]

# Generate a random maze path using Depth-First Search (DFS)
def generate_maze(x, y):
    maze[y][x] = 1

    directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    random.shuffle(directions)

    for dx, dy in directions:
        new_x, new_y = x + 2 * dx, y + 2 * dy
        if 0 <= new_x < num_cols and 0 <= new_y < num_rows and maze[new_y][new_x] == 0:
            maze[y + dy][x + dx] = 1
            generate_maze(new_x, new_y)

generate_maze(0, 0)

# Create an image to represent the maze
cell_size = path_width
image_width = maze_width
image_height = maze_height
image = Image.new("RGB", (image_width, image_height), "white")
draw = ImageDraw.Draw(image)

# Draw the maze walls and paths
for row in range(num_rows):
    for col in range(num_cols):
        if maze[row][col] == 1:  # Changed condition from 0 to 1
            x0, y0 = col * cell_size, row * cell_size
            x1, y1 = x0 + cell_size, y0 + cell_size
            draw.rectangle([x0, y0, x1, y1], fill="black")  # Set path to black
        else:
            x0, y0 = col * cell_size, row * cell_size
            x1, y1 = x0 + cell_size, y0 + cell_size
            draw.rectangle([x0, y0, x1, y1], fill="white")  # Set walls to white

# Save the maze as a printable JPEG
maze_filename = "maze.jpg"
image.save(maze_filename, "JPEG")
print(f"Maze saved as {maze_filename}")

I'm not sure the logic I've applied is correct.

0

There are 0 best solutions below