inpolygon

Tests which points lie inside or on the edge of a polygon.

Category: Mathematics > Computational Geometry

Syntax

in = inpolygon(xq, yq, xv, yv)

Tells whether each query point (xq, yq) is inside or on the polygon with vertices (xv, yv).

[in, on] = inpolygon(___)

Also returns which points lie exactly on the polygon boundary.

Description

A point counts as inside when a ray from it crosses the polygon an odd number of times. Points exactly on an edge or vertex are reported as inside in in and also flagged in on, so in & ~on selects strictly interior points.

The polygon may be concave or self-intersecting; regions covered an even number of times are treated as outside.

Input arguments

xq, yq
Coordinates of the query points; arrays of the same size.
xv, yv
Vectors with the polygon's vertices. The polygon is closed automatically.

Output arguments

in
Logical array the size of xq: true for points inside or on the boundary.
on
Logical array: true for points on the boundary.

Example

Inside, on the edge, outside

xv = [0 1 1 0];
yv = [0 0 1 1];
[in, on] = inpolygon([0.5 1 2], [0.5 0.5 2], xv, yv)
Output
in = 1×3 logical array

   1   1   0
on = 1×3 logical array

   0   1   0

See also

isinterior polyshape polyarea convhull