function EEGCP = evenGroupCenters2d(ng)

% eegrpcntrs(ng) -- Generate random points in the 2D plane, suitable to be 
%  group centers of evenly-spaced groups.
%
% Function for generating a set of random points as a 2 x N
% matrix, where the first value is the x-coord of that individual,
% and the second value is the y-coord of that individual.
% 
% 
% Input parameter is:
% ng = Number of points (groups) to be generated.  
% Maximum is 100

L = 30 * sqrt(pi);          % Length of the field; based on a circle of r=3 BL
                            %  for each fish.  Same for W (width of field).
n = 100 / ng;               % Gets the number per group, n.
s = sqrt(n);                
spac = L / s;               % Spacing constant; see evenptsf.
plac = ( .5 * (s - 1));     % Placement constant; see evenptsf.
md = plac * spac;           % Gets min. distance between groups from the 
                            %  evenptsf constants. 
maxL = L-plac;              % Define max coord. values, so that group-member
maxW = L-plac;              %  points will not be placed off the plane.

% First, we generate a matrix ng x ng wide, and fill it with numbers
% astronomically large. These will then be replaced with actual
% distances.

DIST = 9999 * ones(ng,ng);


% Next, we generate an array, 3 columns long, of individual %'s and
% their X- and Y- coordinates. Again, we do this by starting with 
% all zeros.

PTS = zeros(ng, 2);


% Now, we generate random points, and check them to make sure
% the minimum distance between pts is at least 'md' units

for i = 1:ng            % loop over all members of the group
    
    mindist = 0;
    
    % keep generating randing numbers until the minimum NND
    % is > md
    
    while (mindist <= md)  
        X = maxL * (2*rand-1);        % generate an X value from -(.5L) to (.5L)
        Y = maxW * (2*rand-1);        % generate a Y value from -(.5W) to (.5W)
        
        if i > 1                % Shorts the loop the first time through, since

                                % the first point cannot violate md.
                                
            for j = 1:i-1              % loop over all the previously-generated guys
                DIST(i,j) = dist2d(X,  Y, PTS(j,1), PTS(j,2));
            end
            
            mindist = min(DIST(i,:));
        else 
            mindist = 999;
        end
    end         		        
    
    % Now we've found a point that's not too close. We can
    % add it to the PTS matrix
    
    %     PTS(i,1) = round(i);    % assign the index number to individual i
    PTS(i,1) = X;           % assign X as the x-coord of individual i
    PTS(i,2) = Y;           % assign Y as the y-coord of individual i
    
end                       

EEGCP = PTS;
%plot(EEGCP(:,1),EEGCP(:,2),'.')


