function A = convexpoly(XY,CXY); % This routine accepts one or more matrices each with 2 columns (for % x,y coordinates) and then computes convex polygons around them. % It computes the area of each polygon, creates a plot of it (or them) % and computes the area of overlap of the any subset of polygons. % Rulon Clark, Martin Schlaepfer, and Jack Bradbury. Cornell University, % December 2000. dyp=50; np=17; tp=['ko';'ro';'bo';'go';'mo';'co';'k+';'r+';'b+';'g+';'m+';'c+';'k*';'r*';'b*';'g*';'m*';'c*']; tc=['k';'r';'b';'g';'m';'c']; A=[]; NH=nargin; % Determines whether 1 or multiple files submitted if NH==1 d1x=XY(:,1); %pulls out the x coordinates of matrix d1y=XY(:,2); %pulls out y coordinates of matrix c1=convhull(d1x,d1y); %pulls out the points on the periphery of the mcp cx=d1x(c1); cy=d1y(c1); figure('NumberTitle','off','Name','Minimum Convex Polygon') hold on plot(d1x,d1y,'bo'); %plots the points on the figure plot(cx,cy,'b'); %plots the lines connecting the points in c1 xlabel('X axis'); ylabel('Y axis'); hold off A(1)=polyarea(cx,cy); tx=num2str(A(1)); disp(' '); tx=['Area of minimum convex polygon: ' tx]; disp(tx); disp(' '); else [n,m]=size(XY); % Begin multi-input overlap routines NM=max(size(CXY)); % Get number of additional animals provided NN=NM+1; CH=cell(NN,1); disp(' '); dopts=1; rsp=input('1. Include points in plots (1) or not (2): ','s'); if (rsp=='2') dopts=0; end disp('2. Area of minimum convex polygons: '); tx1=' File '; for i=1:NN tx=num2str(i); if (i==1) txlb=[tx1 tx]; XY1=XY; figure('NumberTitle','off','Name','Minimum Convex Polygons'); nfigs=1; hold on; else XY1=CXY{(i-1)}; tx2=[tx1 tx]; txlb=char(txlb,tx2); end dx=XY1(:,1); dy=XY1(:,2); c=convhull(dx,dy); % Find indices of hull coordinates cx=dx(c); cy=dy(c); A(i)=polyarea(cx,cy); % Store area of each polygon tx=[' ' txlb(i,:) ': ' num2str(A(i))]; disp(tx); p=[cx cy]; % Get hull coordinates for this polygon CH{i}=p; % Store polygon coordinates in cell array j=mod(i,6); k=mod(i,18); if (dopts==1) plot(dx,dy,tp(k,:)); %plots the points on the figure end plot(p(:,1),p(:,2),tc(j)); %plots the lines connecting the points in p end xlabel('x axis'); ylabel('y axis'); disp(' '); input(' Click mouse to place UL of legend and hit ..'); pt=get(gca,'CurrentPoint'); pt=pt(1,1:2); ppt=pt; yl=get(gca,'YLim'); legdy=(yl(2)-yl(1))/dyp; for j=1:NN tf=text(pt(1),pt(2),txlb(j,:)); set(tf,'Color',tc(mod(j,6))); R=get(tf,'Extent'); pt(2)=R(2)-legdy; end hold off xrange=get(gca,'XLim'); yrange=get(gca,'YLim'); disp(' '); disp('3. Overlap computations:'); more=1; while (more==1) vv=[]; ff=[]; tfx=' Overlap for Files '; disp(' Select files to include (1) or exclude (2): '); nf=0; for i=1:NN tq=[' ' txlb(i,:) ': ']; rsp=input(tq,'s'); if (rsp=='1') nf=nf+1; vv(nf)=i; end end disp(' '); tfx=[tfx ':']; OP=[]; if (nf>1) OP=CH{vv(1)}; tx=num2str(vv(1)); tx=['Overlap for Files ' tx ', ']; for i=2:nf j=vv(i); tx1=num2str(j); if (i