R/helperFunctions.R

# Calculate the neighbours of a cell based on list number
neighbour <- function(i, width, height, maze) {

  # pretty inefficient, should be done without maze TODO!!!!!!!!!!!!!!!!!!!!!
  x <- maze[i, "x"]
  y <- maze[i, "y"]

  res <- NULL

  # Calculate the neighbours and save them in res
  ifelse(y + 1 <= height, res <- c(res, list_nr(x, y + 1, width)), res <- c(res,0))
  ifelse(x + 1 <= width , res <- c(res, list_nr(x + 1, y, width)), res <- c(res,0))
  ifelse(y - 1 >  0     , res <- c(res, list_nr(x, y - 1, width)), res <- c(res,0))
  ifelse(x - 1 >  0     , res <- c(res, list_nr(x - 1, y, width)), res <- c(res,0))

  return(unname(res))
}


# Calculate the list entry number based on x/y coordinates (1,1 being bottomleft)
list_nr <- function(x, y, width) {
  return(x + (y - 1)* width)
}

# Calculate the x and y coordinates based list number.
#
coord_from_list_nr <- function(nr, width, height){
  row_nr <- height - (nr - 1) %/% width
  col_nr <- (nr - 1) %% width + 1
  return(c(row_nr,col_nr))
}

# Calculate which cell wall has to be deleted if given two cell list numbers
# returns 1 for N, 2 for E, 3 for S, 4 for W
which_wall <- function(cell1, cell2, height){
  if(cell2 - cell1 ==  1) return(2)
  if(cell2 - cell1 == -1) return(4)
  if(cell2 - cell1 ==  height) return(1)
  if(cell2 - cell1 == -height) return(3)
}

# Visualization of the maze via index number
# x = 1 y = 1 would be 1
# x = 2 y = 1 would be 2
###############
# 13 14 15 16 #
# 09 10 11 12 #
# 05 06 07 08 #
# 01 02 03 04 #
###############
Ziegelsteintom/rmazing documentation built on May 10, 2019, 1:58 a.m.