%% Points Inside Multiply Connected Polygon
% Find the points inside a square with a square hole.
% Define a square region with a square hole. Specify the vertices of the outer loop in a counterclockwise direction, and specify the vertices for the inner loop in a clockwise direction. Use NaN to separate the coordinates for the outer and inner loops.

xv = [1 4 4 1 1 NaN 2 2 3 3 2];
yv = [1 1 4 4 1 NaN 2 3 3 2 2];

%
% Define x and y coordinates of 500 random points. Initialize the random-number generator to make the output of randn repeatable.

rng default
xq = rand(500,1)*5;
yq = rand(500,1)*5;

%
% Determine whether each point lies inside or on the edge of the polygon area.

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

%
% Plot the polygon and the query points. Display the points inside the polygon with a red plus. Display the points outside the polygon with a blue circle.

figure

plot(xv,yv,'LineWidth',2) % polygon
axis equal

hold on
plot(xq(in),yq(in),'r+') % points inside
plot(xq(~in),yq(~in),'bo') % points outside
hold off

% Query points in the square hole are outside the polygon.
% Copyright 2015 The MathWorks, Inc.