function DPTS = randPtsNormal2d(n); % DRandompts(n) - Produces n random points in a circular field, which have % a decreasing probability of being placed farther from the center. The points % will be normally distributed around the center of the group, with the x and y % coordinates each having a mean of 0 and a SD of R/3, where R is the max % radius of the group. % Min. distance between points is set at 1, which is 1 body length, for % generality. Here, the size limit of the group is calculated as a circle % with radius = R. It is derived by giving each point to be generated an % area of 3.125*pi square body lengths, yielding a new total area which is % the circle mentioned. R = sqrt(n*3.125); md = 1; % Initialize the DIST matrix, which is used to check md's later. DIST = 9999 * ones(n,n); % Initialize the PTS matrix, which will be filled by the x-y coords of % the generated points. PTS = zeros(n, 4); % Now the points are generated. for i = 1:n % Loop over each point to be made. mindist = 0; % Initialize mindist while (mindist <= md) % Keep generating until mindist is less % than the designated min. distance. % x and y coords are based on the distance from the center, (0,0), % which is designated r. r is a random no.from a normal distribution % with SD = R/3. r = (R/3) * randn; theta = 2 * pi * rand; % A random angle between 1 and 2pi. x = r * sin(theta); % x and y are placed along angle y = r * cos(theta); % theta, r distance from center. d2c = dist2d(0,0,x,y); % Checks the distance to center. % This loop checks to make sure that the point is not outside the % max group radius; if it is, a new point is generated. while ( d2c > R ) r = (R/3) * randn; theta = 2 * pi * rand; x = r * sin(theta); y = r * cos(theta); d2c = dist2d(0,0,x,y); end % Now, a check is made that md is not violated. if i > 1 % Shorts the loop the first time through, since % the first point cannot violate md. for j = 1:i-1 DIST(i,j) = dist2d(x, y, PTS(j,2), PTS(j,3)); 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,2) = x; % assign X as the x-coord of individual i PTS(i,3) = y; % assign Y as the y-coord of individual i end DPTS = PTS; % Final product renamed. % plot(DPTS(:,2),DPTS(:,3),'.') % axis([-30,30,-30,30])