Title: | Utilities for Making and Plotting Mazes |
---|---|
Description: | Functionality for generating and plotting random mazes. The mazes are based on matrices, so can only consist of vertical and horizontal lines along a regular grid. But there is no need to use every possible space, so they can take on many different shapes. |
Authors: | Kelly Street [aut, cre] |
Maintainer: | Kelly Street <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.5 |
Built: | 2024-10-25 05:48:19 UTC |
Source: | https://github.com/kstreet13/mazing |
A function to decrease the size of a matrix while attempting to preserve the original values.
condense_matrix(m, fun = median)
condense_matrix(m, fun = median)
m |
A |
fun |
A function for summarizing four values that condenses them down to
one value. Default is |
This function can be used for image manipulation and is included for
the purpose of making mazes from images. PNG images can be read in as
arrays via png::readPNG
and if the image is too big, this funtion
can help get it down to a more manageable size.
A matrix with dimensions half the size of the original matrix, where each value is a summary of the four corresponding values in the original.
m <- outer(1:4, 1:6) condense_matrix(m)
m <- outer(1:4, 1:6) condense_matrix(m)
A function to double the size of a matrix, with each cell in the original becoming four cells in the expanded matrix.
expand_matrix(m)
expand_matrix(m)
m |
A |
A matrix with the same distribution of values as the original, but doubled in size along both dimensions.
m <- matrix(1:6, nrow = 2) expand_matrix(m)
m <- matrix(1:6, nrow = 2) expand_matrix(m)
A function that takes a description of a point in a maze and finds the matrix indices corresponding to that point.
find_maze_refpoint(point, maze)
find_maze_refpoint(point, maze)
point |
A description of a relative position in the maze, such as
|
maze |
A |
For standard values of point
, this function will identify a "target"
(such as the top left corner of the matrix) and select the point in the maze
that is closest to that target, by Euclidean distance. The standard choices
for point
are: "topleft"
, "top"
, "topright"
,
"righttop"
, "right"
, "rightbottom"
,
"bottomright"
, "bottom"
, "bottomleft"
,
"leftbottom"
, "left"
, "lefttop"
, and "center"
.
For convenience, there are several redundancies built in; for example,
"topleft"
is identical to "lefttop"
.
In addition to the standard values, there is a complementary set of "matrix"
or "manhattan-like" values, each of which is prepended with an "m"
(for example, "mtopleft"
). These options select the most extreme value
along one dimension (ie. the highest possible row), fix that value, and then
select the local extreme in the other dimension (column). For example, the
value "mtopleft"
will select the highest possible row in the maze
before selecting the left-most point in that row. Note that this means values
such as "mtopleft"
and "mlefttop"
are not synonymous.
For convenience, if point
is a 2-column matrix or numeric
vector that can be coerced into a 2-column matrix, it will be returned as
such, with minor formatting changes to match the usual output. This is so
that functions which rely on find_maze_refpoint
can use either
relative descriptions or exact coordinates.
A matrix of integers with 2 columns, giving the coordinates of the desired point(s). Note that the x-coordinate (column index) comes first, so for the corresponding index in the original matrix, these coordinates will need to be reversed.
m <- maze(15,15) r <- find_maze_refpoint('topright', m) plot(m, walls = TRUE) points(r[1], r[2], col = 2, pch = 16)
m <- maze(15,15) r <- find_maze_refpoint('topright', m) plot(m, walls = TRUE) points(r[1], r[2], col = 2, pch = 16)
Functions for producing and identifying procedurally generated random mazes.
maze(nrow, ncol) is.maze(x) as.maze(x)
maze(nrow, ncol) is.maze(x) as.maze(x)
nrow |
Number of rows. |
ncol |
Number of columns. |
x |
A matrix-like object to be made into a maze (for |
For as.maze
, if x
is a binary matrix (or otherwise
contains only two unique values), the maze will be constrained to occupy
only those cells containing a 1 (the higher value).
For maze
and as.maze
, an object of class maze
, which is
a subclass of matrix
.
For is.maze
a logical value indicating if the input is a valid
maze
.
[print.maze()]
maze(10,10) mat <- matrix(NA, 10, 10) is.maze(mat) m <- as.maze(mat) is.maze(mat) # circular maze mat <- matrix(1, 30, 30) for(i in 1:nrow(mat)){ for(j in 1:ncol(mat)){ if((i-15.5)^2+(j-15.5)^2 > 220){ mat[i,j] <- 0 } } } m <- as.maze(mat)
maze(10,10) mat <- matrix(NA, 10, 10) is.maze(mat) m <- as.maze(mat) is.maze(mat) # circular maze mat <- matrix(1, 30, 30) for(i in 1:nrow(mat)){ for(j in 1:ncol(mat)){ if((i-15.5)^2+(j-15.5)^2 > 220){ mat[i,j] <- 0 } } } m <- as.maze(mat)
A function to convert a maze object into a binary matrix. This can be useful for visualization (as when plotting the walls of the maze) and for constructing complex mazes, such as a maze-within-a-maze.
maze2binary(m)
maze2binary(m)
m |
A |
A binary matrix where values of 1 denote paths through the maze and values of 0 denote the walls (impassable regions) of the maze.
m <- maze(10,10) m2 <- maze2binary(m)
m <- maze(10,10) m2 <- maze2binary(m)
Plot a maze object
## S3 method for class 'maze' plot(x, walls = FALSE, ...) ## S3 method for class 'maze' lines( x, walls = FALSE, adjust = c(0, 0), openings = NULL, openings_direction = NULL, ... )
## S3 method for class 'maze' plot(x, walls = FALSE, ...) ## S3 method for class 'maze' lines( x, walls = FALSE, adjust = c(0, 0), openings = NULL, openings_direction = NULL, ... )
x |
A |
walls |
logical value, indicating that the walls of the maze should be
plotted, rather than the paths through the maze. Default is |
... |
Additional arguments passed to |
adjust |
A vector by which to adjust the overall position of the maze.
Should be a numeric vector of length 2, indicating the |
openings |
Locations in the maze where a certain wall(s) should not be
drawn (only applies when |
openings_direction |
Character vector describing which walls should not
be drawn at the locations specified by |
When plotting, the coordinates for locations in the maze are taken from the indices of the corresponding locations in the matrix representation. This means that the plot will appear to be flipped vertically relative to the matrix representation of the maze.
The openings_direction
argument specifies where to create an
opening (ie. where not to draw walls), relative to a location in the maze
given by openings
. Possible values are: "left"
,
"right"
, "top"
, "bottom"
, "topleft"
,
"topright"
, "bottomright"
, "bottomleft"
, and
"all"
. If not specified, this will be set as the first value in
c("left","right","top","bottom")
to reference a wall that would
otherwise have been drawn.
Returns NULL
, invisibly.
m <- maze(10,10) plot(m, walls = TRUE) lines(m, lwd = 5, col = 3)
m <- maze(10,10) plot(m, walls = TRUE) lines(m, lwd = 5, col = 3)
Display a maze
object.
## S3 method for class 'maze' print(x, ..., walls = FALSE, start = NULL, end = NULL, compress = "v")
## S3 method for class 'maze' print(x, ..., walls = FALSE, start = NULL, end = NULL, compress = "v")
x |
A |
... |
Further arguments passed to
|
walls |
logical value, indicating that the walls of the maze should be
plotted, rather than the paths through the maze. Default is |
start , end
|
The coordinates of the starting and ending points of a path
through the maze, or descriptions of relative locations (eg.
|
compress |
How to compress the bitmap representation, see
|
If the bittermelon
package is available,
print.maze
prints a representation of the maze az a bitmap object.
Otherwise, it prints the matrix representation.
m <- maze(10,10) print(m)
m <- maze(10,10) print(m)
A function that finds the shortest path between points in a maze.
solve_maze(maze, start = "bottomleft", end = "topright")
solve_maze(maze, start = "bottomleft", end = "topright")
maze |
A |
start |
The coordinates of the starting point, or a description of a
relative location (see |
end |
The coordinates of the end point, or a description of a relative
location (see |
For the start
and end
arguments (as well as the output
matrix), these coordinates refer to the plotting coordinates, not the
matrix indices. For plotting, the x-coordinate (column index) is listed
first, whereas in matrix notation, the row (y-coordinate) is listed first.
A matrix
containing the coordinates of the path through the
maze. Note that the x-coordinate (column index) comes first, so for the
corresponding indices in the original matrix, these coordinates will need
to be reversed.
m <- maze(15,15) p <- solve_maze(m) plot(m, walls = TRUE) lines(p, col = 2, lwd = 3)
m <- maze(15,15) p <- solve_maze(m) plot(m, walls = TRUE) lines(p, col = 2, lwd = 3)
A function to spread a particular value into neighboring cells of a matrix. When that matrix is a binary representation of a maze, this can be used to widen the paths of the maze (subsequently narrowing the walls).
widen_paths(m, value = 1, blocked = NULL, square_corners = FALSE)
widen_paths(m, value = 1, blocked = NULL, square_corners = FALSE)
m |
A |
value |
The value to be expanded into neighboring cells. For widening the paths of the maze, this is the value that represents paths. |
blocked |
Values that should be preserved, regardless of whether or not the expansion would normally encroach on their cells. |
square_corners |
Whether or not to include diagonals in the expansion. Including diagonals will preserve square corners, whereas excluding them (default) creates more rounded looking corners. |
The idea of this function is to let the paths of a maze "seep" into
the walls. For making a maze-within-a-maze, the walls of the big maze are
generally not as interesting as the paths, which contain smaller paths.
Hence why we might want to make the paths wider than the walls. Remember
that this will affect walls from both sides, so it is necessary to expand
the matrix twice (via expand_matrix
) before widening the
paths for the first time.
A matrix with the same dimensions as the original, in which cells that border a cell of a particular value
have been assigned that value
.
m <- matrix(0, nrow = 5, ncol = 5) m[3,3] <- 1 widen_paths(m) widen_paths(m, square_corners = TRUE) m[,2] <- 2 widen_paths(m, blocked = 2)
m <- matrix(0, nrow = 5, ncol = 5) m[3,3] <- 1 widen_paths(m) widen_paths(m, square_corners = TRUE) m[,2] <- 2 widen_paths(m, blocked = 2)