Learn R Programming

secr (version 4.6.10)

pointsInPolygon: Points Inside Polygon

Description

Determines which of a set of points lie inside a closed polygon or at least one of a set of polygons

Usage

pointsInPolygon(xy, poly, logical = TRUE)

Value

Vector of logical or integer values, one for each row in xy

Arguments

xy

2-column matrix or dataframe of x-y coordinates for points to assess

poly

2-column matrix or dataframe containing perimeter points of polygon, or a SpatialPolygonsDataFrame object from package sp, or a `mask' object (see Warning)

logical

logical to control the output when `poly' is a mask (see Details)

Warning

If poly is a mask object then its cells must be aligned to the x- and y- axes

Details

If poly is a SpatialPolygonsDataFrame object then the method over is used from sp. This allows multiple polygons and polygons with holes.

If poly is an secr `mask' object then xy is discretized and matched to the cells in poly. If logical = FALSE then the returned value is a vector of integer indices to the row in `poly' corresponding to each row of `xy'; otherwise the result is a vector of logical values.

Otherwise, the algorithm is adapted from some code posted on the S-news list by Peter Perkins (23/7/1996). The polygon should be closed (last point same as first).

See Also

Examples

Run this code

## 100 random points in unit square
xy <- matrix(runif(200), ncol = 2)
## triangle centred on (0.5, 0.5)
poly <- data.frame(x = c(0.2,0.5,0.8,0.2), y = c(0.2,0.8,0.2,0.2))
plot(xy, pch = 1 + pointsInPolygon(xy, poly))
lines(poly)

Run the code above in your browser using DataLab