%%%%%%%%%%%%% Polygons Detection %%%%%%%%%%%%%%%%

clear;clc;
%%%%%%%%%%% Randomly generate two convex polygons %%%%%%%%%%%%%%%
n = 8;
x = 100*rand(n^2,1);
y = 100*rand(n^2,1);
%plot(x,y, '.');
k = convhull(x,y);
Poly1 = x(k)+j*y(k);
Poly1 = Poly1.';

%%%%%%%%%%%%%%%%% two particle are seperated away from each other%%%%%%%%%%
space = 100*(0.7+j*0.6)+(randn(1,1)+j*randn(1,1));

x = 100*rand(n^2,1);
y = 100*rand(n^2,1);
%plot(x,y, 'g.');
k = convhull(x,y);
Poly2 = space+x(k)+j*y(k);
Poly2 = Poly2.';

%%%%%%%%% Two matrix with size Nx2 N----# of points  column1----x coordinates column2----y
Matrix1 = [real(Poly1); imag(Poly1)];
Matrix1 = Matrix1(:,1:length(Poly1)-1);

Matrix2 = [real(Poly2); imag(Poly2)];
Matrix2 = Matrix2(:,1:length(Poly2)-1);



[intsctFlag CombineShapeMatrix] = DetectIntersectionPolygons(Matrix1,Matrix2);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Show the resul%%%%%%%%%%%%%%%%%%
figure; 
subplot(1,2,1);
plot(Poly1,'rx-'); hold on;% plot(C1,'m^');hold on;
plot(Poly2,'b+-');hold on;%plot(C2,'cv');hold on; 
title 'Two input particles'
if intsctFlag
    CombineShapeBack=Convert2PlotFormat(CombineShapeMatrix);
    plot(CombineShapeBack,'gs');
    subplot(1,2,2); plot(CombineShapeBack,'kx-');
    title 'Combine Particle'
else
end