Find Points Inside a Given Polygon

DESCRIPTION:

Determine whether points are inside a polygon.

USAGE:

points.in.poly(x, y, polygon) 

REQUIRED ARGUMENTS:

x
the X-coordinates of the points
y
the Y-coordinates of the points. Must be the same length as x.
polygon
a list with named components "x" and "y".

VALUE:

a logical vector the same length as x. If TRUE then the corresponding point is inside the given polygon and so on.

BUG:

if a ray from a point to an edge intersects a horizontal edge, i.e. is collinear with it, the C program will return TRUE even if such point is not in the polygon.

SEE ALSO:

, .

EXAMPLES:

# 100 points on a unit square 
x <- runif(100); y <- runif(100) 
# A square polygon in the center: 
pcenter <- list(x=c(.25,.25,.75,.75), y=c(.25,.75,.75,.25)) 
pin <- points.in.poly(x, y, pcenter) 
# Plot the unit square and the center square: 
plot(x, y, type='n'); polygon(pcenter, density=0, col=2) 
# Plot only the points in the center square: 
points(x[pin], y[pin], col=3)