%% Points Inside Self-Intersecting Polygon
% Define the x and y coordinates for a pentagram.

xv = [0.5;0.2;1.0;0;0.8;0.5];
yv = [1.0;0.1;0.7;0.7;0.1;1];

%
% Define the x and y coordinates of 12 query points.

xq = [0.1;0.5;0.9;0.2;0.4;0.5;0.5;0.9;0.6;0.8;0.7;0.2];
yq = [0.4;0.6;0.9;0.7;0.3;0.8;0.2;0.4;0.4;0.6;0.2;0.6];

%
% Determine whether each point lies inside or on the edge of the polygon area. Also determine whether any of the points lie on the edge of the polygon area.

[in,on] = inpolygon(xq,yq,xv,yv);

%
% Determine the number of points lying inside or on the edge of the polygon area.

numel(xq(in))

%
% Determine the number of points lying on the edge of the polygon area.

numel(xq(on))

%
% Determine the number of points lying outside the polygon area (not inside or on the edge).

numel(xq(~in))

%
% Plot the polygon and the points. Display the points strictly inside the polygon with a red plus. Display the points on the edge with a black asterisk. Display the points outside the polygon with a blue circle.

figure

plot(xv,yv) % polygon

hold on
plot(xq(in&~on),yq(in&~on),'r+') % points strictly inside
plot(xq(on),yq(on),'k*') % points on edge
plot(xq(~in),yq(~in),'bo') % points outside
hold off

% Six points lie inside the polygon. Two points lie on the edge of the polygon. Four points lie outside the polygon.
% Copyright 2015 The MathWorks, Inc.