function EGCP = evenGroupCenters2dV02(ng) % egrpcntrs(ng) -- Generate random points in the 2D plane, suitable to be % group centers, of even-sized 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. L = 30 * sqrt(pi); % Length of the field; based on a circle of r=3 BL % for each fish. Width of field is equivalent. npg = 100 / ng; r = sqrt(3.125 * npg); md = 4 * r; maxL = L-r; maxW = L-r; % 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 DIST; EGCP = PTS; % plot(EGCP(:,1),EGCP(:,2),'.')