R/plotMaze.R

#' Plot Method for Maze Object
#'
#' Plots a maze
#'
#' @param x an object of class maze
#' @param ... optional arguments for the plot function
#'
#' @return NULL
#' @export
#'
#' @examples
#' maze <- makeMazeRecursiveBacktracker(height = 10, width = 10, seed = 42, start = 1)
#' plot(maze)

plot.maze <- function(x, ...) {

  object <- x
  w <- attributes(object)$width
  h <- attributes(object)$height

  # for a square plot with no distortion
  m <- max(w,h)

  # Save graphic options
  pmar <- graphics::par("mar")

  graphics::par(pty = "s")
  graphics::par(mar = c(1,1,1,1))
  graphics::plot(NULL, xlim = c(0,m +1), ylim = c(0,m + 1), type = "n", axes = FALSE,
                 xlab = "", ylab = "", ...)

  for(i in 1:nrow(object)){

    x <- object[i, "x"]
    y <- object[i, "y"]
    if(object[i,1]) graphics::lines(c(x, x + 1), c(y + 1, y + 1))
    if(object[i,2]) graphics::lines(c(x + 1, x + 1), c(y, y + 1))
    if(object[i,3]) graphics::lines(c(x, x + 1), c(y, y))
    if(object[i,4]) graphics::lines(c(x, x), c(y, y + 1))

  }

  # Restore Graphic options
  graphics::par(pty = "m")
  graphics::par(mar = pmar)

}
Ziegelsteintom/rmazing documentation built on May 10, 2019, 1:58 a.m.