#' Plot Method for a Distance Object
#'
#' Adds a rainbow colored interpretation of the distance matrix
#'
#' @param x a distance matrix
#' @param ... optional graphics parameter
#' @param alpha controls how bright the colors will be
#' @param startingpoint boolean, if TRUE it will plot the starting point of
#' of the distance matrix calculations
#'
#' @return NULL
#' @export
#'
#' @examples
#' maze <- makeMazeRecursiveBacktracker()
#' d <- distance(maze)
#' plot(maze)
#' plot(d)
plot.distance <- function(x, alpha = 0.3, startingpoint = TRUE, ...){
w <- attributes(x)$width
h <- attributes(x)$height
# for some reason putting attr() inside the fct does not work
s <- attributes(x)$start
# Maximum distance
m <- max(x)
# Initialize result vector
res <- vector(mode = "list", length = m)
# Calculate Cells for all distances
for(i in 1:m){
res[[i]] <- which(x == i, arr.ind = TRUE)
}
color <- grDevices::rainbow(m, alpha = alpha)
# Loop over distances
for(i in 1:m){
# Cells with same distance
for(k in 1:nrow(res[[i]])){
x <- res[[i]][k,2]
y <- h - res[[i]][k,1] + 1
graphics::polygon(x = c(x, x+1, x+1, x),
y = c(y, y, y+1, y+1),
col = color[i], border = NA, ...)
}
}
# Draw starting point
if(startingpoint == TRUE){
temp_coord <- coord_from_list_nr(s, w, h)
xcoord <- temp_coord[2] + 0.5
ycoord <- h + 1.5 - temp_coord[1]
graphics::points(x = xcoord , y = ycoord, pch = 20)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.