Maze generator with most popular shapes - hexagon, triangle, square

Overview

Maze-Generator

Maze generator with most popular shapes - hexagon, triangle, square (sqaure not implemented yet):

  1. Theory:
  • Planar Graph https://en.wikipedia.org/wiki/Planar_graph Its role is to make the structure of the maze. In this graph information is stored information about nodes, edges and faces of the grid. Nodes are (x, y) points position, edges (a, b) contains indexes of nodes which create specific edge. Faces are a lists of lists of edges [(a, b), (b, c), ..., (f, a)] which create specific face.

  • Dual Graph https://en.wikipedia.org/wiki/Dual_graph Its role is to make the grid of the possible moves between cells (faces of the planar graph)

  • k-nearest neighbour algorithm https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm Is used for determining neighbour cells for each cell

  • randomized depth-first search algorithm https://en.wikipedia.org/wiki/Maze_generation_algorithm Is used for determining possible moving ways in the maze. It is just one of many possibilities to make ways. I choosed recursive implementation

  • edges intersection https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ Testing wheter edges are intersecting using vectors theory. Orientation of two vectors (edges) is computed and is tested whether intersect point is a part of the segments. Implementation is took from the link. It is neccessary to test whether planar graph's wall is intersecting with possible way (dual graph's edges). If yes, wall is deleted.

  1. Idea:
  • Program is built based on graph theory. Maze is made of two graphs. The idea is to make a planar graph which creates grid of shapes (hexagons, triangles etc.) and create another graph which is dual to the first graph. The role of this second graph is to create possible moving ways in the maze. This graph has nodes in the centers of faces defined by the planar graph and edges, which connecting every neighbour face with k-nearest neighbour algorithm. The next step is to run Randomized depth-first search algorithm which travels around the dual graph and creates actual maze. The last step is to delete walls which are intersected by the edges of the dual graph.
  1. Program's structure:

3.1 In first step the planar graph is constructed with possibility to change the number of columns and rows of the maze. This creates a structure of the maze (e.g. 10x10):

image

  • In prepareGraph() function is called Hex() function (columns * rows) times which creates (columns * rows) hexagons. Hex() called multiple times creates duplicated nodes and edges in the graph which is undesirable, so in the next part of the prepareGraph function duplicated nodes are removed and the edges indexes are repaired (because after deleting some nodes, edges has references to nodes which actually do not exist (look 1. Theory links))

3.2 Second step is construction dual graph to the planar graph. It creaetes structure of the possible moves to choose by the algorithm (red one, case 10x10):

image

  • Dual graph F is created in DualGraph() function which takes another Graph G which it will be dual to. Nodes of graph F are computed by calculating center of each face in graph G ((average(sum(each x)), average(sum(each y)), each x and y from all nodes which create specific face). After that, edges are defined with k-nearest-neighbours algorithm. It takes 6 nearest candidates to be a neighbour and make edge between nodes which are in distance < 3 * a. Thats because not every node has 6 neighbours, so this restriction decreasing number of neighbours for side hexagons.

3.3 Next step is to make actual maze. To do this I used randomized depth-first search algorithm (one of many possibilities, one of the easiest and which gives simplest mazes):

image

  • Firstly, algorithm selects random node from dual graph (red one) and marks it as visited and add it to possible backtrack. Then it makes list with possible ways (edges) to unvisited nodes and selects random way. If unvisited node in nearest neighbourhood does not exists (dead end) it backtracks (move to the last node at the backtrack list and removes this node from this list). If unvisited node or nodes exist algorithm selects it randomly and marks new node to the visited nodes. Then add this node to the "Backtrack" list and add selected way to the "journey" list which contains traveled ways. After each completed iteration (without dead ends) algorithm checks for all the edges if the nodes which are connected by the edge are both visited. If yes, it tests if this edge is in the "journey". If not, it can be deleted.

3.4 Last step is to delete walls between cells connected by dual graph's edges (10x10 case):

image

  • Function DeleteIntersections() deletes all the walls (planar graph's edges) which are intersecting with the ways (dual graph's edges). For every edge in dual graph it takes every edge in planar graph and tests whether the edges intersect. If yes, the wall is deleted and another way is took to test (because way could intersect with only one wall so it has no sense to continue this iteration). To tell if the edges are intersected I use functions doIntersect() which uses orientation(), which returns orientation of the given vectors, and onSegment which tells us whether the intersect point is in the range of the vectors.
  1. Some information
  • It is possible to turn off display of the dual graph (ways), just comment last but one for loop

image

  • Program is inefficient for large mazes (32x32 is constructing 30sec), so it is recommended to test program for max 20x20 size :)

  • Changing shape's edge size to enough small value (a = 1) could cause some bugs in dual graph's structure

  • To get the best effect in triangle maze call prepareGraph with columns and rows values in ratio 3:2 (18x12, 24x16 etc.)

    image

You might also like...
A not exist person image generator python module

A not exist person image generator python module

Panel Competition Image Generator
Panel Competition Image Generator

Panel Competition Image Generator This project was build by a member of the NFH community and is open for everyone who wants to try it. Relevant links

Random collage/montage generator with drop-shadow
Random collage/montage generator with drop-shadow

Random Collage Example Usage These are the sample input files in $PWD for the below examples: 1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10

Archive of the image generator stuff from my API

alex_api_archive Archive of the image generator stuff from my API FAQ Q: Why? A: Because I am removing these components from the API Q: How do I run i

Ascify-Art - An easy to use, GUI based and user-friendly colored ASCII art generator from images!
Ascify-Art - An easy to use, GUI based and user-friendly colored ASCII art generator from images!

Ascify-Art This is a python based colored ASCII art generator for free! How to Install? You can download and use the python version if you want, modul

Avatar Generator Python

This is a simple avatar generator project which uses your webcam to take pictures and saves five different types of your images into your device including the original image.

Qrgenerator - A qr generator app using python3
Qrgenerator - A qr generator app using python3

qrgenerator by Mal4D Hi welcome into qr code generator using python by Mal4d Lin

NFT collection generator. Generates layered images

NFT collection generator Generates layered images, whole collections. Provides additional functionality. Repository includes three scripts generate.py

Fixed Version Of Blender Low Poly Rock Generator For Blender 3.0.0
Fixed Version Of Blender Low Poly Rock Generator For Blender 3.0.0

Blender (3.0.0) - Low Poly Rock Generator This is an addon for Blender 3.0.0 to generate low poly rocks. It was based on an addon that unfortunately h

Comments
  • Triangle maze problem for small amount of triangles in grid

    Triangle maze problem for small amount of triangles in grid

    For small triangle mazes 15x10 and smallers some triangles does not have a way to the neighbours when creating dual graph:

    image

    This is caused probably by bug in adding faces when creating planar graph (blue one). It is always one triangle and this specific triangle has wrong edges (edges are in different places in the grid), so center of this triangle is computed wrongly too. This causes that the node in dual graph which should be way to this triangle is outside it, so this triangle is skipped.

    opened by CamaroTheBOSS 1
Owner
Kacper Plesiak
Student of the Gdańsk University of Technology in the field of Control Engineering. Interested in Machine Learning, Chess and Football
Kacper Plesiak
An ascii art generator that's actually good. Does edge detection and selects the most appropriate characters.

Ascii Artist An ascii art generator that's actually good. Does edge detection and selects the most appropriate characters. Installing Installing with

null 18 Jan 3, 2023
QR-Generator - An awesome QR Generator to create or customize your QR's

QR Generator An awesome QR Generator to create or customize your QR's! Table of

Tristán 1 Jan 28, 2022
Python avatar generator for absolute nerds

pagan Welcome to the Python Avatar Generator for Absolute Nerds. Current version: 0.4.3 View the change history here. Remember those good old days whe

David Bothe 280 Dec 16, 2022
Python QR Code image generator

Pure python QR Code generator Generate QR codes. For a standard install (which will include pillow for generating images), run: pip install qrcode[pil

Lincoln Loop 3.5k Dec 31, 2022
Combinatorial image generator for generative NFT art.

ImageGen Stitches multiple image layers together into one image. Run usage: stitch.py [-h] <backgrounds_dir> <dinos_dir> <traits_dir> <texture_file> <

Dinosols NFT 19 Sep 16, 2022
Samila is a generative art generator written in Python

Samila is a generative art generator written in Python, Samila let's you create arts based on many thousand points. The position of every single point is calculated by a formula, which has random parameters. Because of the random numbers, every image looks different.

Sepand Haghighi 947 Dec 30, 2022
A Robust Avatar Generator with a huge number of templates

CoolAvatars Welcome to this repository of CoolAvatars. Using this project, you can generate cool avatars not only from the samples present in my image

RAVI PRAKASH 5 Oct 12, 2021
Create a QR-code Generator app using only Python.

QR-code_Generator Create a QR-code Generator app using only Python. This apps generated a QR code for a single link. Libraryes used in this app --> py

Soham P Phasalkar 1 Oct 17, 2021
Python Digital Art Generator

Python Digital Art Generator The main goal of this repository is to generate all possible layers permutations given by the user in order to get unique

David Cuentas Mar 3 Mar 12, 2022
QR Generator using GUI with Tinker

BinCat Token System Very simple python script with GUI that generates QR codes. It don't include a QR "decription" tool. It only generate-it and thats

Hipotesi 1 Nov 6, 2021