Find n random points with a minimum distance r inside a 2D rectangular box

조회 수: 18 (최근 30일)
jaydeep 2014년 7월 18일
편집: Jan 2017년 2월 21일
I would like to generate the (x, y) coordinates of N randomly distributed points within a 2D Square box having 2000 m X 2000m. the points must have a minimum distance of 200m from each other, means a second point should not lies between the area of circle covered by the first point and so on. Any idea for a uniform random distribution?

댓글을 달려면 로그인하십시오.

답변 (3개)

John BG 2017년 2월 12일
편집: John Kelly 2017년 2월 17일
Hi Jaydeep
just completed a couple functions that may help with your question
1.
from
2.
run this
[X,Y,Nmax,Dmatrix]=scatter_points7
3.
key in the menu the rectangle size, the amount of points you want and the minimum distance.
4.
scatter_points7 returns:
X Y coordinates of the random points
Nmax the maximum amount of points that would fit in the rectangle if placing them orderly.
Dmatrix the distances between all combinations of points.
5.
Check the minimum distance requirement is met with
% test 1
Ap=20;L=combinator(Ap,2,'c');
relD2=((X(L(:,2))-X(L(:,1))).^2+(Y(L(:,2))-Y(L(:,1))).^2).^.5
R0=200;find(relD2<R0)
=
Empty matrix: 1-by-0
or
% test 2
Ap=20;L2=combinator(Ap,2);
D2=Dmatrix+NaN*eye(Ap);
R0=200;D2(D2<R0)
=
Empty matrix: 0-by-1
.
'
6.
Now try this
[X,Y,Nmax,Dmatrix]=scatter_points_saturate(2000,2000,200)
.
.
to know the amount of points (below Nmax) actually generated
numel(X)
John BG
댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Doug Hull 2014년 7월 18일
편집: Doug Hull 2014년 7월 18일
Here is something stupid that just might work.
While NOT_DONE
generate a point
If NOT too close to existing points
Place point
end
check to see if enough points are placed.
end
댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Jan 2017년 2월 12일
편집: Jan 2017년 2월 21일
function [X, Y, D] = GetPointsRandom(nWant, XWidth, YWidth, MinDist)
X = zeros(nWant, 1);
Y = zeros(nWant, 1);
dist_2 = MinDist ^ 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;
newY = YWidth * rand;
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
% Throw an error, if the area is filled too densely:
if nValid < nWant
error('Cannot find wanted number of points in %d iterations.', iLoop)
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by