%% Polygon with Hole
% Analyze polygons with nested boundaries.
% Create a polygon with two properly nested boundaries. polyshape defines the outermost boundary as the exterior bound of a solid region. Working inward, the next boundary defines the start of a hole.

t = 0.05:0.5:2*pi;
x1 = cos(t);
y1 = sin(t);
x2 = 0.5*cos(t);
y2 = 0.5*sin(t);
pgon = polyshape({x1,x2},{y1,y2})
plot(pgon)

%
% Use the addboundary function to create a second polygon that adds a third, outer boundary to pgon.

x3 = 2*cos(t);
y3 = 2*sin(t);
pgon2 = addboundary(pgon,x3,y3)
plot(pgon2)

%
% Adding a third, outer boundary requires polyshape to reorganize the solid and hole boundaries based on the new nesting pattern. Again, polyshape starts with the outermost boundary that indicates the start of a solid region, then alternates between hole and solid with each nested boundary, working inward. The new polygon now has two solid regions and one hole.
% Copyright 2012 The MathWorks, Inc.