R/poly_area.R

Defines functions poly_area

Documented in poly_area

NULL
#' Calculate the area of the polygon with the given vextices
#' @param x Numeric. An array contains the x coordinates of the vextices.
#' @param y Numeric. An array contains the y coordinates of the vextices.
#' @return The area of the polyon.
#' @export

# the code is inspired by the EFDC code
poly_area <- function(x, y){
  polygon.n <- length(x)
  area <- 0
  vec.x <- vec.y <- rep(0, 2)
  vec.x[1] <- x[2] - x[1]
  vec.y[1] <- y[2] - y[1]
  for (i in 3:polygon.n){
    vec.x[2] <- x[i] - x[1]
    vec.y[2] <- y[i] - y[1]
    area <- area + 0.5 * abs(vec.x[1] * vec.y[2] - vec.x[2] * vec.y[1])
    vec.x[1] <- vec.x[2]
    vec.y[1] <- vec.y[2]
  }
  return(area)
}
hxfan1227/efdcr documentation built on July 21, 2023, 9:50 p.m.