[Vegan-commits] r936 - pkg/vegan/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Aug 21 14:38:48 CEST 2009
Author: jarioksa
Date: 2009-08-21 14:38:45 +0200 (Fri, 21 Aug 2009)
New Revision: 936
Modified:
pkg/vegan/R/summary.ordihull.R
Log:
simpler way of calculating the area of a polygon
Modified: pkg/vegan/R/summary.ordihull.R
===================================================================
--- pkg/vegan/R/summary.ordihull.R 2009-08-21 06:35:29 UTC (rev 935)
+++ pkg/vegan/R/summary.ordihull.R 2009-08-21 12:38:45 UTC (rev 936)
@@ -1,29 +1,15 @@
-### Centres and areas of convex hulls. The area of the *convex* polygon
-### is found as the *sum* of the areas of triangles.
+### Centres and areas of convex hulls (simple polygons).
`summary.ordihull` <-
function(object, ...)
{
- ## The area of triangle from vertices using eq. 8.10 of Spiegel,
- ## Liu & Lipschitz (1999), Mathematical Handbook of Formulas and
- ## Tables (2nd ed.), McGraw & Hill. The hull is closed, so that
- ## first and last point are identical.
- triarea <- function(x) {
- ones <- rep(1, 3)
- if (nrow(x) < 4)
+ polyarea <- function(x) {
+ n <- nrow(x)
+ if (n < 4)
return(0)
- if (nrow(x) == 4)
- return(abs(det(cbind(x[-1,], ones))/2))
- else {
- sol <- 0
- cnt <- colMeans(x[-1,])
- for (i in 2:nrow(x)) {
- mat <- cbind(rbind(cnt, x[(i-1):i,]), ones)
- sol <- sol + abs(det(mat)/2)
- }
- return(sol)
- }
+ else
+ abs(sum(x[-n,1]*x[-1,2] - x[-1,1]*x[-n,2]))/2
}
- areas <- sapply(object, function(x) triarea(x))
+ areas <- sapply(object, function(x) polyarea(x))
cnts <- sapply(object, function(x) colMeans(x[-1,]))
rbind(cnts, `Area` = areas)
}
More information about the Vegan-commits
mailing list