function C = polymultilayer(x, y, d)
% x, y = [mm] coordinates of the polygon's boundaries
% d = [mm] boundary distance & hatch line spacing
% C = [mm] length of hatching lines
%
%% 微信公众号:数学模型(MATHmodels)
% 联系方式(微信号)KingOfModels
figure('position',[50,50,400,600])
p0 = polyshape(x, y); % Create polyshape object
plot(p0) % Plot polyshape
box on; hold on; axis image;
set(gca,'YDir','reverse')
pj = p0; j = 1;
C = 0; % Initialization length of hatching lines
title(['length of hatching lines = ', num2str(C)])
while pj.NumRegions
% expands the boundaries of a polyshape by a distance -d*j. When d is
% negative, solid boundaries shrink and hole boundaries grow.
pj = polybuffer(p0,-d*j);
C = C + perimeter(pj); % Get the perimeter of expanded polyshape
pj.plot() % Plot polyshape
title(['length of hatching lines = ', num2str(C)]);
j = j + 1; % add layer
drawnow
pause(0.5)
end