This post is the start of a series of posts. It shows how you can draw amazing mazes with Mathematica.

It uses algorithms from the great book Mazes for Programmers by Jamis Buck.

It is currently in beta but can be bought from The Pragmatic Bookshelf.

This time we will start with the Binary Tree Algorithm.

### The Binary Tree Algorithm

Each cell contains two bits to signal, if there is a wall to the right or at top.

Every cell chooses at random, if it should have a right or top wall.

Only the topmost and rightmost cells have no walls.

If you want to know why this works, please look at the book.

The result is mirrored, because the coordinate system in Mathematica increases from bottom to top.

### Drawing the maze

First we need methods to generate the lines for the walls.

Then we have a method, that creates the lines for any cell.

We can apply this function to each cell.

To draw a box around the maze, we need its width and height.

Putting it all together, the following code draws the maze.

Here is the result.