scatter random points on a circle with a minimum distance

Hi Matlab users,
I am trying to write a code to randomly scatter points on a cirlce which these points should have a minimum distant on this circle.
R=1;
for i=1:100
theta(i) = 2*pi*rand();
r(i) = R*sqrt(rand());
x = r.*cos(theta);
y = r.*sin(theta);
end
plot(x,y,'*')
axis equal
This code just creates the points. I want to add a condition that not two points can be closer than (0.005*R) to eachother.
Best,
Argu

 Risposta accettata

Image Analyst
Image Analyst il 11 Mar 2019
Modificato: Image Analyst il 11 Mar 2019
Try this accept/reject method similar to what you've tried:
fontSize = 20;
R=1;
minAllowableDistance = 0.005 * R;
requiredNumberOfPoints = 100;
pointCount = 0;
for k = 1 : 100 * requiredNumberOfPoints
thisTheta = 2*pi*rand();
thisRadius = R * rand();
% Compute distances to other points.
thisx = thisRadius .* cos(thisTheta);
thisy = thisRadius .* sin(thisTheta);
if k == 1
% We can ALWAYS place/accept the first point.
pointCount = pointCount + 1; % Increment count.
x(pointCount) = thisx;
y(pointCount) = thisy;
else
% Compute distances to other points
theseDistances = sqrt((x-thisx) .^ 2 + (y - thisy) .^ 2);
if min(theseDistances) >= minAllowableDistance
pointCount = pointCount + 1; % Increment count.
% We can ALWAYS place/accept the first point.
x(pointCount) = thisx;
y(pointCount) = thisy;
distances(pointCount) = min(theseDistances);
% Bail out if we have enough points.
if pointCount >= requiredNumberOfPoints
break;
end
end
end
end
fprintf('Placed %d points after %d attempts.\n', pointCount, k);
% Plot points.
subplot(2, 1, 1);
plot(x, y, 'b*')
xlim([-R, R]);
ylim([-R, R]);
axis('equal', 'square');
grid on;
hold on;
% Plot circle
pos = [-R, -R, 2*R, 2*R];
rectangle('Position',pos,'Curvature',[1 1])
xlabel('X', 'fontSize', fontSize);
ylabel('Y', 'fontSize', fontSize);
caption = sprintf(' %d Random points in a circle.', requiredNumberOfPoints);
title(caption, 'fontSize', fontSize);
% Plot histogram of distances.
subplot(2, 1, 2);
histogram(distances, 15);
grid on;
title('Histogram of Closest Distances', 'fontSize', fontSize);
You can also do this without a for loop and do it vectorized.
Note the histogram is the histogram as the points were being placed, and is not going to be the same as the histogram of the final set of points. To do that, you can use pdist2() and pass the result into histogram().

8 Commenti

Dear Image Analyst,
this is great, I was also trying to write the code. But you are very fast and much brighter. The only thing is you need to use
thisRadius = R * sqrt(rand());
because otherwise you would have most of the points accumulated in the center.
I highly appreciate your kind answer
Best,
Argu
Sorry, you're right. Thanks for accepting the answer.
Dear Image Analyst,
I would also want to have the "theta" and "R" corresponding to the accepted points(which we have the x and y of them).
Thanks
So, in the else block, just save them
theta(pointCount) = thisTheta;
radius(pointCount) = thisRadius;
Another way would be to just use x and y and don't evey use r and theta and just compute r and theta afterwards from x and y if you need them.
I add it into else block, I changed the required number to 70 but this gives me 69 points. am I missing the first point?
Those two lines have to go everywhere right after it says
y(pointCount) = thisy
in other words, in both the k==1 block and the nested if block.
Argumanh
Argumanh il 11 Mar 2019
Modificato: Argumanh il 11 Mar 2019
I got it, thank you very much
Dear Image Analyst
I have added a new question which it needs a little modification on this code, I would appreciate if you could take a look at it.
Best,
Argu

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by