For a given matrix of points coordinates, pointsInPolygon returns a logical vector stating whether or not these points are inside a specific polygon whose coordinate as passed as an argument.

pointsInPolygon(points, polygon)

Arguments

points

a matrix of coordinates of points to be tested.

polygon

a two-columns matrix including the coordinate of the polygon.

Details

Implements the Ray-casting algorithm.

References

https://rosettacode.org/wiki/Ray-casting_algorithm#C

Examples

mat <- matrix(10*runif(100), 50) res <- pointsInPolygon(mat, cbind(c(4,8,8,4),c(4,4,8,8))) # Visual assessment plot0(c(0,10), c(0,10))
polygon(c(4,8,8,4),c(4,4,8,8))
graphics::points(mat[,1], mat[,2], col=res+1)