


PURPOSE: create a set of random points within coordinate domain X/Y
-------------------------------------------------------------------
USAGE: PS = random_points(n, X, Y, mindist, fitnodes);
where: [n] are the number of points to create
[X] is the X coordinate vector
[Y] is the Y coordinate vector. Cellsize and corner coordinates are
calculated from X and Y vectors
[mindist] is the minimum distance between random points in grid
units (cellsize). Default is 1.
0 as mindist might create multiple points with same
coordinates.
[fitnodes] (optional) if set to 1, random points coordinates will
always be at center of coordinate vector nodes, thus be
aligned on a regular grid. Default is 1.
-------------------------------------------------------------------------
OUTPUTS:
[PS] n x 2 matrix: rows = number of points with cols X and Y coords.
-------------------------------------------------------------------------
Notes: Points will lie strictly within bounds of given min-max X and
min-max Y. Consider if giving center or llc coords!
Felix Hebeler, Geography Dept., University Zurich, March 2006.

0001 function PS = random_points(n, X, Y, mindist, fitnodes) 0002 % PURPOSE: create a set of random points within coordinate domain X/Y 0003 % ------------------------------------------------------------------- 0004 % USAGE: PS = random_points(n, X, Y, mindist, fitnodes); 0005 % where: [n] are the number of points to create 0006 % [X] is the X coordinate vector 0007 % [Y] is the Y coordinate vector. Cellsize and corner coordinates are 0008 % calculated from X and Y vectors 0009 % [mindist] is the minimum distance between random points in grid 0010 % units (cellsize). Default is 1. 0011 % 0 as mindist might create multiple points with same 0012 % coordinates. 0013 % [fitnodes] (optional) if set to 1, random points coordinates will 0014 % always be at center of coordinate vector nodes, thus be 0015 % aligned on a regular grid. Default is 1. 0016 % ------------------------------------------------------------------------- 0017 % OUTPUTS: 0018 % [PS] n x 2 matrix: rows = number of points with cols X and Y coords. 0019 % ------------------------------------------------------------------------- 0020 % Notes: Points will lie strictly within bounds of given min-max X and 0021 % min-max Y. Consider if giving center or llc coords! 0022 % 0023 % Felix Hebeler, Geography Dept., University Zurich, March 2006. 0024 if exist('mindist','var') 0025 else 0026 mindist=1; 0027 end 0028 if exist('fitnodest','var') 0029 else 0030 fitnodes=1; 0031 end 0032 %% Intialise parameters 0033 llc=[min(X), min(Y)]; % (center) coords of lower left corner cell 0034 urc=[max(X), max(Y)]; 0035 xsize=urc(1)-llc(1); 0036 ysize=urc(2)-llc(2); 0037 %xcs=abs(X(2)-X(1));ycs=abs(Y(2)-Y(1)); %x and y direction cellsize 0038 0039 PS=nan(n,2); 0040 PS(:,1)=2*xsize; %initialise pointset with points out of bounds, so dist function can be used in loop 0041 PS(:,2)=2*ysize; 0042 0043 %% loop 1 to n 0044 if (fitnodes==0) 0045 if mindist==0 % if mindist is 0, just create points without checking 0046 PS(:,1)=rand(n,1)*numel(X)*xsize+llc(1); PS(:,2)=rand(n,1)*numel(X)*ysize+llc(2); 0047 else % check that points are separated by mindist 0048 PS(1,1)=rand*xsize+llc(1); PS(1,2)=rand*ysize+llc(2); %first point 0049 i=1; 0050 while i<n; 0051 check=0; 0052 i=i+1; 0053 while check==0; % while new point is too close to existing point, create new coords 0054 PS(i,1)=rand*xsize+llc(1); PS(i,2)=rand*ysize+llc(2); 0055 check=1; 0056 for j=1:i-1 %only calc the points created by now 0057 D=dist([PS(i,:)',PS(j,:)']); % check if dist to existing points > mindist 0058 if D(1,2)<mindist % if too close, reset flag and break to get new coords 0059 check=0; 0060 break 0061 end 0062 end 0063 end 0064 if mod(100,floor(n/i))==0 0065 disp(['Points created:',num2str(i)]); 0066 end 0067 end 0068 end 0069 else % if fitnodes==1 0070 if mindist==0 % if mindist is 0, just create points without checking 0071 PS(:,1)=floor(rand(n,1)*numel(X)+0.5)*xsize+llc(1); PS(:,2)=floor(rand(n,1)*numel(Y)+0.5)*ysize+llc(2); 0072 else % check that points are separated by mindist 0073 0074 PS(1,1)=floor(rand*numel(X)+0.5)*xsize+llc(1); PS(1,2)=floor(rand*numel(Y)+0.5)*ysize+llc(2); %first point 0075 i=1; 0076 while i<n; 0077 check=0; 0078 i=i+1; 0079 while check==0; % while new point is too close to existing point, create new coords 0080 PS(i,1)=floor(rand*numel(X)+0.5)*xsize+llc(1); PS(i,2)=floor(rand*numel(Y)+0.5)*ysize+llc(2); 0081 check=1; 0082 for j=1:i-1 %only calc the points created by now 0083 D=dist([PS(i,:)',PS(j,:)']); % check if dist to existing points > mindist 0084 if D(1,2)<mindist % if too close, reset flag and break to get new coords 0085 check=0; 0086 break 0087 end 0088 end 0089 end 0090 if mod(100,floor(n/i))==0 0091 disp(['Points created:',num2str(i)]); 0092 end 0093 end 0094 end 0095 end 0096 display(['Points created:',num2str(n),' - Done.']); 0097 %% output debug 0098 %figure, plot(PS(:,1),PS(:,2),'r.')