%% Sort by Perimeter
% Identify and access individual regions of a polygon by sorting them first.
%
% Create a polygon with two solid regions and one hole.

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

%
% Sort the regions of the polygon in ascending order based on their perimeter. You then can use the regions function to create an array of polyshape objects R, where each element of R corresponds to a single region of polyout. The elements of R are indexed in the same order as the regions of polyout, so that you can access and compute with each region based on their index.

polyout = sortregions(polyin,'perimeter','ascend');
R = regions(polyout);
plot(R)
smallregion = area(R(1))
bigregion = area(R(2))

% Copyright 2012 The MathWorks, Inc.