R/polys.r

Defines functions Polycenter Polyarea

Documented in Polyarea Polycenter

Polyarea <- function(x) {
 x <- rbind(x, x[1, ])
 n <- nrow(x)
 if (n < 4) return(0)
 else  abs(sum(x[-n, 1] * x[-1, 2] - x[-1, 1] * x[-n, 2]))/2
}

# ===

Polycenter <- function(x) {
 x <- rbind(x, x[1, ])
 n <- nrow(x)
 if (n < 4) return(colMeans(x[-n, , drop=FALSE]))
 xy <- x[-n, 1] * x[-1, 2] - x[-1, 1] * x[-n, 2]
 A <- sum(xy)/2
 xc <- sum((x[-n, 1] + x[-1, 1]) * xy)/A/6
 yc <- sum((x[-n, 2] + x[-1, 2]) * xy)/A/6
 structure(c(xc, yc), names=colnames(x))
}

Try the shipunov package in your browser

Any scripts or data that you put into this service are public.

shipunov documentation built on Feb. 16, 2023, 9:05 p.m.