R/patchnumber_from_position.R

Defines functions patchnumber_from_position

patchnumber_from_position <- function(x,y,patch_X,patch_Y){

  extr_x <- extremum(x)
  extr_y <- extremum(y)
  N_X <- ceiling((extr_x[2]-extr_x[1])/patch_X)
  N_Y <- ceiling((extr_y[2]-extr_y[1])/patch_Y)
  N <- N_X*N_Y

  compt <- 1
  patch <- rep(0,length(x))
  for (ix in seq(N_X)){
    for (iy in seq(N_Y)){
      pos <- which(x >= (extr_x[1] + (ix-1)*patch_X) &
                     x < (extr_x[1] + (ix)*patch_X) &
                     y >= (extr_y[1] + (iy-1)*patch_Y) &
                     y < (extr_y[1] + (iy)*patch_Y))
      patch[pos] <- compt
      compt <- compt + 1
    }
  }

  return(patch)
}
femeunier/LianaRTM documentation built on Dec. 20, 2021, 7:50 a.m.