Fractal Mazes The maze generation algorithm used here is a variant of the Recursive Backtracking algorithm which creates a maze by exploring a grid and removing walls between cells in a randomized manner About this fractal maze algorithm 1 Fractal Nature The fractal nature of this maze arises from the fact that the maze generation process creates a complex structure with self similarity at different scales Each cell in the maze has four possible walls top right bottom left and these walls are removed randomly to connect neighboring cells As the maze is explored and walls are removed intricate patterns emerge resembling the self similar patterns seen in fractals 2 Algorithm Overview The maze generation starts at a specific cell usually the top left corner and explores the grid recursively At each cell the algorithm randomly chooses a neighboring cell that has not been visited yet If such a cell exists it moves to that cell removes the wall between the current cell and the chosen neighbor and continues the exploration recursively from the chosen neighbor If there are no unvisited neighboring cells the algorithm backtracks to the previous cell until it finds a cell with unvisited neighbors This process continues until all cells in the grid have been visited resulting in a maze with interconnected paths and dead ends 3 Fractal Levels In the provided implementation each cell is assigned a fractal level which represents its distance from the outer edges of the maze The fractal level is calculated based on the Manhattan distance of the cell from the nearest edge This distance is used to determine the color of the cell with different colors representing different fractal levels By assigning colors based on the fractal level we can visually distinguish regions of the maze that are closer to or farther away from the outer edges highlighting the fractal nature of the maze The maze generated by this algorithm exhibits fractal properties due to its self similar structure at different scales The Recursive Backtracking algorithm explores the grid and removes walls between cells to create interconnected paths while the assignment of colors based on fractal levels helps visualize the self similarity of the maze structure DOCTYPE html html head title Fractal Maze title style canvas border 1px solid black style head body canvas id mazeCanvas width 400 height 400 canvas script const canvas document getElementById mazeCanvas const ctx canvas getContext 2d const cellSize 20 Size of each cell const cols Math floor canvas width cellSize const rows Math floor canvas height cellSize const colors FF5733 FFC300 36D7B7 3498DB 9B59B6 Different colors for fractal levels Create a 2D array to represent the maze const maze Array from length rows Array from length cols visited false walls true true true true Function to draw the maze function drawMaze ctx clearRect 0 0 canvas width canvas height for let y 0 y rows y for let x 0 x cols x const cell maze y x const fractalLevel Math min x y cols x 1 rows y 1 drawCell x y fractalLevel cell Function to draw a cell with walls function drawCell x y fractalLevel cell const walls cell ctx fillStyle colors fractalLevel ctx fillRect x cellSize y cellSize cellSize cellSize ctx strokeStyle black ctx lineWidth 2 if walls 0 Top wall ctx beginPath ctx moveTo x cellSize y cellSize ctx lineTo x 1 cellSize y cellSize ctx stroke if walls 1 Right wall ctx beginPath ctx moveTo x 1 cellSize y cellSize ctx lineTo x 1 cellSize y 1 cellSize ctx stroke if walls 2 Bottom wall ctx beginPath ctx moveTo x 1 cellSize y 1 cellSize ctx lineTo x cellSize y 1 cellSize ctx stroke if walls 3 Left wall ctx beginPath ctx moveTo x cellSize y 1 cellSize ctx lineTo x cellSize y cellSize ctx stroke Recursive Backtracking algorithm to generate the maze function generateMaze x y maze y x visited true const neighbors shuffleNeighbors x y for const neighbor of neighbors const nx ny direction neighbor if ny 0 ny rows nx 0 nx cols maze ny nx visited maze y x walls direction false maze ny nx walls direction 2 4 false generateMaze nx ny Helper function to shuffle an array function shuffle array for let i array length 1 i 0 i const j Math floor Math random i 1 array i array j array j array i return array Helper function to get shuffled neighboring cells function shuffleNeighbors x y const neighbors nx x ny y 1 direction 0 Top nx x 1 ny y direction 1 Right nx x ny y 1 direction 2 Bottom nx x 1 ny y direction 3 Left return shuffle neighbors Generate the maze and draw it generateMaze 0 0 drawMaze script body html
ractal levels Create a 2D array to represent the maze const maze Array from length rows Array from length cols visited false walls true true true true Function to draw the maze function drawMaze ctx clearRect 0 0 canvas width canvas height for let y 0 y rows y for let x 0 x cols x const cell maze y x const fractalLevel Math min x y cols x 1 rows y 1 drawCell x y fractalLevel cell Function to draw a cell with walls function drawCell x y fractalLevel cell const walls cell ctx fillStyle colors fractalLevel ctx fillRect x cellSize y cellSize cellSize cellSize ctx strokeStyle black ctx lineWidth 2 if walls 0 Top wall ctx beginPath ctx moveTo x cellSize y cellSize ctx lineTo x 1 cellSize y cellSize ctx stroke if walls 1 Right wall ctx beginPath ctx moveTo x 1 cellSize y cellSize ctx lineTo x 1 cellSize y 1 cellSize ctx stroke if walls 2 Bottom wall ctx beginPath ctx moveTo x 1 cellSize y 1 cellSize ctx lineTo x cellSize y 1 cellSize ctx stroke if walls 3 Left wall ctx beginPath ctx moveTo x cellSize y 1 cellSize ctx lineTo x cellSize y cellSize ctx stroke Recursive Backtracking algorithm to generate the maze function generateMaze x y maze y x visited true const neighbors shuffleNeighbors x y for const neighbor of neighbors const nx ny direction neighbor if ny 0 ny rows nx 0 nx cols maze ny nx visited maze y x walls direction false maze ny nx walls direction 2 4 false generateMaze nx ny Helper function to shuffle an array function shuffle array for let i array length 1 i 0 i const j Math floor Math random i 1 array i array j array j array i return array Helper function to get shuffled neighboring cells function shuffleNeighbors x y const neighbors nx x ny y 1 direction 0 Top nx x 1 ny y direction 1 Right nx x ny y 1 direction 2 Bottom nx x 1 ny y direction 3 Left return shuffle neighbors Generate the maze and draw it generateMaze 0 0 drawMaze script body html