function RPTS = randpts3d(n, L, W, H, md ) % randpts3d(n, L, W, H, md) -- Generate random points in the 3D plane. % % Function for generating a set of random points as a 3 x N % matrix, where the first value is the point % (fish %1, bird %3, etc), % the second value is the x-coord of that individual, and the third % value is the y-coord of that individual. % %Created by Jim Work, University of South Carolina, August 13th, 2003 % Input parameters are: % n = Number of points to be generated % L = length of the field % W = width of field % H = height of the field % md = minimum distance allowable between individuals ("personal space") % % First, we generate a matrix n x n wide, and fill it with numbers % astronomically large. These will then be replaced with actual % distances. DST = zeros(n, n); for i = 1: n % loop over all individuals, i for j = 1:n % loop over all other individual, j DIST(i,j) = 9999; end % terminate j loop end % terminate i loop % 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(n, 4); % Set up the random number seed so that we have true, rather than pseudo, random % numbers (note that if you don't do this, the random numbers start off the same % every time you restart MatLab). We use clock as the variable, since the system % clock will be different every time this function is called. rand('state',sum(100*clock)); % 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) X = L * rand; % generate an X value from 0 to L Y = W * rand; % generate a Y value from 0 to L Z = H * rand; for j = 1:i-1 % loop over all the previously-generated guys DIST(i,j) = dist3d(X, Y, Z, PTS(j,2),PTS(j,3), PTS(j,4)); end mindist = min(DIST(i,:)); end % end k 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 PTS(i,4) = Z; % assign Z as the z-coord of individual i end % terminate i loop RPTS = PTS;