function CPTS = circleFish2d(n) % Circlefish(n) - generates n random points within a circle. Radius % is defined by the number of points to be generated. R = sqrt(n * 3.125); md = 1; % Next, we generate a matrix n x n wide, and fill it with numbers % astronomically large. These will then be replaced with actual % distances. DIST = 9999 * ones(n, n); % Next, we generate an array, 4 columns long, of individual %'s and % their X- and Y- coordinates. Again, we do this by starting with % all zeros. PTS = zeros(n, 4); % Now, we generate random points, and check them to make sure % the minimum distance between pts is at least 'md' units for i = 1:n % loop over all members of the group mindist = 0; % keep generating randing numbers until you the minimum NND % is > md. while (mindist <= md) % Start by generating points randomly inside of a square % area (this is the easiest way to generate a coordinate % pair). X = R * (2 * rand - 1); % generate an X value from -R to R Y = R * (2 * rand - 1); % generate a Y value from -R to R % Now we check to make sure that the point is inside the % circular area (that is, it is <= R radius away from % the center point, (0,0). If it is not, we throw it out % and re-generate the points. d2c = dist2d(0,0,X,Y); while ( d2c > R ) % It's inside the square but outside the circle % circumscribed by the square. Thus we must regenerate % the points. X = R * (2 * rand - 1); % generate new X Y = R * (2 * rand - 1); % generate new Y % Check again to make sure it's inside the circle as well % as the square. d2c = dist2d(0,0,X,Y); end 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,2),PTS(j,3)); end mindist = min(DIST(i,:)); else mindist = 999; end end % end loop % 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 % terminate loop CPTS = PTS; % plot(CPTS(:,2),CPTS(:,3),'.') % axis([-30,30,-30,30])