如果您可以将椭圆和蓝色区域转换为SpatialPolygons
对象,那么使用rgeos包中的函数来计算它们的面积、它们的交点面积以及所有其他类型的有趣量就很容易了。
不幸的是,以正确的形式获取对象需要一些繁重的工作:
library(car)
library(sp)
library(rgeos)
## Function for creating a SpatialPolygons object from data.frame of coords
xy2SP <- function(xy, ID=NULL) {
if(is.null(ID)) ID <- sample(1e12, size=1)
SpatialPolygons(list(Polygons(list(Polygon(xy)), ID=ID)),
proj4string=CRS("+proj=merc"))
}
## Ellipse coordinates
plot.new() # Needed by ellipse()
ell <- ellipse(center=c(3.5,5),shape=matrix(c(1,.5,.5,2),nrow=2),radius=3)
dev.off() # Cleaning up after plot.new()
## Three rectangles' coordinates in a length-3 list
x <- c(7,4,1)
y <- c(4,6,7)
mx <- max(x) + 1e6
my <- max(y) + 1e6
rr <- lapply(1:3, function(i) {
data.frame(x = c(x[i], x[i], mx, mx, x[i]),
y = c(y[i], my, my, y[i], y[i]))
})
## Make two SpatialPolygons objects from ellipse and merged rectangles
ell <- xy2SP(ell)
rrr <- gUnionCascaded(do.call(rbind, lapply(rr, xy2SP)))
## Find area of their intersection
gArea(gIntersection(ell, rrr))
# [1] 10.36296