MATLAB: RandomPoints &condition distance

circleimage analysisImage Processing Toolbox

Dear
I need to draw a figure that contains 20 randomized points in an area [-100 100]. If we have the distance between two points less than 6 meters .It is necessary to repeat the randamization of the points
Then each point must know its distance from the other points
close all;
clear all;
clc;
set(gca,'xtick',-100:20:100);
set(gca,'ytick',-100:20:100);
axis([-100 100 -100 100]);
X = 200 * rand(1,20) - 100;
Y = 200 * rand(1,20) - 100;
plot(X, Y, 'b*');
grid on
hold on
datacursormode
%calculate the distance between a point and the other points
%exemple the distance between (A and B) & (A and C) &(A and D) & (A and E)...
%also (B and A) & (B and C) &(B and D) & (B and E).......
distance=[];
for i=1:20
dist = sqrt((X(i)-X(:)).^2+(Y(i)-Y(:)).^2);
distance=[distance dist];
end
thank you
[Moved from asnwer section:]
My question is to display an imege of 20 randomized points and if we have the distance between two points <6meters I repeat the randomization of these two points. And after all if the condition is fulfilled. Each point must know the distance with the other 19 points so I want to find a distance matrix in the workspace that contains 20 row and 20 column as indicated my code.
It is varied that in my code I find in the workspace the matrix distance that is to say each point know its distance with respect to the other points but the condition so that if the distance between two points is <6meters is not realized
I would like my question to be clear

Best Answer

Current version, [Last EDITED, 01.02.2017 16:52 UTC]
function [X, Y, D] = GetPointsRandom(nWant, XWidth, YWidth, R)
X = zeros(nWant, 1);
Y = zeros(nWant, 1);
dist_2 = R ^ 2; % Squared once instead of SQRT each time
iLoop = 1; % Security break to yoid infinite loop
nValid = 0;
while nValid < nWant && iLoop < 1e6
newX = XWidth * (rand - 0.5);
newY = YWidth * (rand - 0.5);
if all(((X(1:nValid) - newX).^2 + (Y(1:nValid) - newY).^2) > dist_2)
% Success: The new point does not touch existing points:
nValid = nValid + 1; % Append this point
X(nValid) = newX;
Y(nValid) = newY;
end
iLoop = iLoop + 1;
end
% An error is safer than a warning:
if nValid < nWant
error('Cannot find wanted number of points in %d iterations.', iLoop)
end
% [Edited start] "figure" inserted, 'Parent' used, 'XGrid' inserted:
FigH = figure;
AxesH = axes('XTick', -100:20:100, 'YTick', -100:20:100, ...
'XLim', [-100, 100], 'YLim', [-100, 100], ...
'XGrid', 'on', 'YGrid', 'on', 'NextPlot', 'add', ...
'Parent', FigH);
plot(X, Y, 'b*', 'Parent', AxesH);
% [EDITED end]
if nargout > 2
% D = pdist([X, Y]); % Faster with statistics toolbox
D = sqrt(bsxfun(@minus, X, X.') .^ 2 + bsxfun(@minus, Y, Y.') .^ 2);
end
end
Call this e.g. as:
[X, Y, D] = GetPointsRandom(1000, 1e6, 1e6, 6)
The list of coordinates is expanded, when a new random point does not touch any exitsing points.
If you do not have the Statistics Toolbox for pdist, use e.g. http://www.mathworks.com/matlabcentral/fileexchange/15145-distance-matrix.
The suggested method for the distance matrix computes the full matrix, although it is symmetric. For in the number of points is small (< 10000), this is not tragic.
This rejection method works reliably and fast up to a number of about 700 points for a side length of 200 and a diameter of 6. If it fails due to reaching the iteration limit, check if increasing the limit helps. If not, the area is nearly filled up by circles and there will not be a solution for your problem.