% Script C7
% Checks out the class ConvexPoly
clear
close all
% Parameters to play with...
s = 5; % Polygons will be randomly placed inside
% the square fill([-s s s -s],[-s -s s s],'y')
n = 3; % The number of sides in each polygon
N = 100; % The number of polygons randomly placed in the square
% Generate and display all the polygons...
figure
fill([-s s s -s],[-s -s s s],'y')
axis square
hold on
for k=1:N
P(k) = ConvexPoly(n);
% Random translation...
delX = (s-1)*(-1+2*rand());
delY = (s-1)*(-1+2*rand());
P(k).Translate(delX,delY);
P(k).Show('r')
end
% Tabulate all the overlapping pairs...
Count = zeros(N,N);
for i=1:N
for j=i+1:N
if P(i).Disjoint(P(j))
% The i-th and j-th polygons overlap...
Count(i,j) = 1;
Count(j,i) = 1;
end
end
end
% Count all the off-diagonal 0's and halve...
nOverlaps = (sum(sum(Count==0))-N)/2;
% A column with N-1 1's signals a polygon that is disjoint from all the
% others...
nDisjoint = sum(sum(Count)==N-1);
title(sprintf('#Disjoint = %1d #Overlapping Pairs = %1d',nDisjoint,nOverlaps),...
'fontsize',16)