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;
iLoop = 1;
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)
nValid = nValid + 1;
X(nValid) = newX;
Y(nValid) = newY;
end
iLoop = iLoop + 1;
end
if nValid < nWant
error('Cannot find wanted number of points in %d iterations.', iLoop)
end
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);
if nargout > 2
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.
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.
Best Answer