Create random points in a rectangular domain, but with minimum separation distance
28 views (last 30 days)
Show older comments
Hello,
I am not a regular user of this program. Hence this query.
I am interested to create random points in a 2D rectangular space, to start with, a uniform distribution. I know how to do this. However, I want to ensure that each of these points are separated by a certain minimum distance.
Can anyone help me with the script please.
Thank you.
Suresh
0 Comments
Accepted Answer
Image Analyst
on 12 Oct 2014
You basically have to try and see. If it's too close, reject that point and get a new one.
x = rand(1, 10000);
y = rand(1, 10000);
minAllowableDistance = 0.05;
numberOfPoints = 300;
% Initialize first point.
keeperX = x(1);
keeperY = y(1);
% Try dropping down more points.
counter = 2;
for k = 2 : numberOfPoints
% Get a trial point.
thisX = x(k);
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
keeperX(counter) = thisX;
keeperY(counter) = thisY;
counter = counter + 1;
end
end
plot(keeperX, keeperY, 'b*');
grid on;
7 Comments
Image Analyst
on 27 Apr 2022
@William Harvie, please post a more efficient way to do it if you know of one.
@Prashant Katiyar, try this:
%============================================================================================================================================
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
% Get a list of trial (x,y) circle centers.
x = 100 * rand(1, 1000000);
y = 100 * rand(1, 1000000);
% Specify how many circles are desired.
numberOfCircles = 300;
minRadius = 1;
maxRadius = 5;
% Specify how close the centers may be to each other.
% Should be at least twice the max radius if they are not to touch.
minAllowableDistance = max([11, 2 * maxRadius]);
% Initialize first point.
keeperX = x(1);
keeperY = y(1);
radii = minRadius;
% Try dropping down more points.
counter = 2;
for k = 2 : length(x)
% Get a trial point.
thisX = x(k);
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
% This trial location works. Save it.
keeperX(counter) = thisX;
keeperY(counter) = thisY;
% Get a random radius.
radii(counter) = minRadius + (maxRadius - minRadius) * rand;
% Quit if we have enough or ran out of circles to try
if counter >= numberOfCircles
break;
end
counter = counter + 1;
end
end
% Plot a dot at the centers
plot(keeperX, keeperY, 'b+', 'MarkerSize', 9);
viscircles([keeperX(:), keeperY(:)], radii, 'Color', 'b');
grid on;
axis equal
numCirclesPlaced = length(keeperX);
caption = sprintf('Could place %d circles', numCirclesPlaced);
title(caption, 'fontSize', fontSize)
g = gcf;
g.WindowState = 'maximized'
Joan
on 1 Jul 2022
@Image Analyst, thank you very much for your code. It is showing me some direction. However, I am a bit stuck. Please add some code that would make the circles enclosed inside a polyshape of coordinates say,
xv=[0 0 20 35 66 85 105 105]; % X-Coordinates
yv=[0 20 20 40 40 20 16 0]; % Y-Coordinates
More Answers (2)
SS
on 1 Nov 2014
1 Comment
Image Analyst
on 1 Nov 2014
I suggest you post this in a brand new question. But try the ellipsoid function first using the "try and keep/reject" concept. If you can't get it working, post your code in a new question since it's a different topic than this one.
SS
on 1 Nov 2014
2 Comments
Image Analyst
on 1 Nov 2014
Edited: Image Analyst
on 1 Nov 2014
Yeah but I see you didn't take my advice, so expect a lot of questions like "Do the axes of the ellipsoids have to align with the xyz axes?", "What have you tried?", "Have you seen the ellipsoid function?", "Is this a homework problem?", and so on.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!