%% Difference of Two Polygons
% Create and plot two polygons poly1 and poly2 that partially overlap.

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
poly2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
plot(poly1)
hold on
plot(poly2)
figure

%
% Subtract poly2 from poly1. The resulting polygon is poly1 minus any part of poly2 that overlaps with poly1.

polyout1 = subtract(poly1,poly2)
plot(polyout1)

% Now subtract the two polygons in the opposite order, that is, subtract poly1 from poly2. The resulting polygon is poly2 minus any part of poly1 that overlaps poly2.

polyout2 = subtract(poly2,poly1)
plot(polyout2)
xlim([-0.2 1.4]);
ylim([-0.2 1.2]);

% Copyright 2012 The MathWorks, Inc.