R/mask_donut.R

Defines functions mask_donut

Documented in mask_donut

#' Mask coordinates using donut pertubation
#'
#' Perturbs points with a uniform perturbation in a donut.
#' Note that `r_min` `r_max` can either be one distance, or a distance per data point.
#' @param x coordinates, `matrix` or `data.frame` (first two columns)
#' @param r_min `numeric` min perturbation distance (vectorized)
#' @param r_max `numeric` min perturbation distance (vectorized)
#' @param plot if `TRUE` points will be plotted.
#' @return adapted `x` with perturbed coordinates
#' @example ./example/mask.R
#' @export
#' @family point perturbation
mask_donut <- function(x, r_min, r_max, plot = FALSE){
  N <- nrow(x)

  ra <- r_min + (r_max - r_min) * stats::runif(N)

  a <-  stats::runif(N, max = 2*pi)

  px <- ra * cos(a) + x[,1]
  py <- ra * sin(a) + x[,2]

  if (isTRUE(plot)){
    x_r <- cbind(px,py)
    plot(rbind(x, x_r), type="n", axes = TRUE, xlab="", ylab="", las=1)
    graphics::points(x, col="black", pch=19)
    graphics::grid()
    graphics::points(x_r, col = "red")
  }

  x[,1] <- px
  x[,2] <- py
  x
}


pertubate_donut <- mask_donut
edwindj/sdcSpatial documentation built on April 13, 2025, 1:57 a.m.