Send
Close Add comments:
(status displays here)
Got it! This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. Note: This appears on each machine/browser from which this site is accessed.
R: Maze generation using grid
1. R: Maze generation using grid
This page looks at maze generation in R using grid.
2. Recursive maze generation and visualization
3. Grid package
The grid package for R can be used for custom graphics. Here it is used to help create a recursively-generated maze.
4. Install R package grid
R packages can be installed from R Studio or from the command line. Here is one way to install the R package
grid from the Windows command line using the default Windows installation path for R.
"C:\Program Files\R\R-4.0.4\bin\Rscript.exe" -e "install.packages('grid', repos='https://cran.rstudio.com/')"
The
repo parameter needs to be included and the single quotes need to be used inside the double quotes.
The path you use should include the installed version of R. The one here is
4.0.4.
A Lua command line console program is now presented to create a maze as an image.
This may not be the best Lua program for this task, but is sufficient for the present purposes.
The global variable linesCount1 is used, in part to show how global variables can be used, when appropriate, as some languages require special handling or access mechanisms for global variables.
5. Algorithm pesudo-code
Here is a pseudo-code for the maze generation algorithm as described to make a maze of rows and columns.
Main:
Start with an empty canvas.
Outline the maze limits with an entry and exit point on opposite sides.
Call MazeGenerate with the rows and columns to be filled in.
MazeGenerate:
If the rows and columns requested are both greater than 1 Then
If the height is greater than the width Then
Divide the grid into top and bottom parts.
Draw a (horizontal) line between the parts, leaving a gap to pass through.
Call MazeGenerate on the top part
Call MazeGenerate on the bottom part
Else
Divide the grid into left and right parts.
Draw a (vertical) line between the parts, leaving a gap to pass through.
Call MazeGenerate on the left part
Call MazeGenerate on the right part
End If
End If
The actual code fills in details that are not needed at the higher level of the algorithm description. For example, the side and gap lengths, and exact x and y coordinates are omitted. As another example, the division into parts can be done using pseudo-random numbers, but that is omitted from the algorithm.
For more information, see the following:
Maze algorithm explanation using GIF images
Here is the R code [#1]
Here is the output of the R code.
Here is the image created by the R code.
6. Observations
It was not clear how to cap the lines such that they appear more continuous (as can be done in PostScript and SVG).
Most variables are local so the "<-" assignment is used. For global variables, such as linesCount1, the "<<-" assignment operator is used.
The invisible() function is used to avoid having "ugly" output created. This can be done with return() statements.
The same code notes apply here as applied to the Lua code for the same problem at
Lua: Maze generation with static SVG .
7. End of page