function [diameter, firstendpoint, secondendpoint] = diameter2d(M)

% Finds the longest inter-individual distance for M, an indexed standard 2d set of individuals.
% Can return the endpoints in question.

currentMax = 0;
j=0;
currentep1 = 0;
currentep2 = 0;

for i=1:(size(M,1))
	for j=1:(size(M,1))
		if dist2d(M(i,2), M(i,3),  M(j,2), M(j,3))>currentMax
         currentMax = dist2d(M(i,2), M(i,3), M(j,2), M(j,3));
         currentep1 = M(i,1);
         currentep2 = M(j,1);
    	end
  	end
end

firstendpoint = currentep1;
secondendpoint = currentep2;

diameter = currentMax;
