%% Intersection of Polygon and Line
% Create a rectangular polygon and a line segment.

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
lineseg = [0.5 0.5; 1.5 1.5];

%
% Compute the intersection of the polygon with the line segment, and determine which sections of the line segment are inside or outside of the polygon.

[in,out] = intersect(poly1,lineseg);
plot(poly1)
hold on
plot(in(:,1),in(:,2),'b',out(:,1),out(:,2),'r')
legend('Polygon','Inside','Outside','Location','NorthWest')

% Copyright 2012 The MathWorks, Inc.