Learn R Programming

tigers (version 0.1-3)

is.insidePolygon: Test If a Point Is Inside a Polygon

Description

This function tests if a point is inside a polygon.

Usage

is.insidePolygon(XY, points)

Value

a logical vector indicating whether each point is inside the polygon defined by XY.

Arguments

XY

A two-column matrix giving the coordinates of a polygon.

points

a vector with two values giving the coordinates of a point, or a matrix with two columns.

Author

Emmanuel Paradis

Details

The algorithm is based on ``ray-tracing'': a segment is traced between points and an arbitrary point far from the polygon. If this segment intersects an odd number of edges of the polygon, then points is inside the polygon.

The polygon must be open and can be in either clockwise or counterclockwise order. If the polygon is closed, it is modified internally without warning (the original polygon is not modified).

See Also

is.open

Examples

Run this code
XY <- rbind(c(0, 0), c(0, 1), c(1, 1), c(1, 0))
stopifnot(is.insidePolygon(XY, c(0.5, 0.5)))
stopifnot(!is.insidePolygon(XY, c(1.5, 1.5)))

Run the code above in your browser using DataLab