scatter randomized points with circles
显示 更早的评论
Hello,
How we can draw circles of radius R on randomized points in a area100*100? Knowing that its points are located in the following way:
The minimum distance between all points> 6 meters.
do you have an idea ?
Thank you
1 个评论
Jan
2017-2-14
What is the difference to your question https://www.mathworks.com/matlabcentral/answers/322431-randompoints-condition-distance? Do you have the coordinates of the points already and only want to draw the circles?
采纳的回答
更多回答(2 个)
You can obtain the coordinates from the solution posted here: https://www.mathworks.com/matlabcentral/answers/142322-find-n-random-points-with-a-minimum-distance-r-inside-a-2d-rectangular-box#answer_254413. Then to draw the circles:
nWant = 20;
Dist = 6;
[X, Y] = GetPointsRandom(nWant, 100, 100, Dist);
alpha = linspace(0, 2*pi, 32).';
% cx = X.' + 0.5 * Dist * cos(alpha); % Matlab >= 2016b
% cy = Y.' + 0.5 * Dist * sin(alpha); % Matlab >= 2016b
cx = bsxfun(@plus, X.', 0.5 * Dist * cos(alpha));
cy = bsxfun(@plus, Y.', 0.5 * Dist * sin(alpha));
figure;
AxesH = axes('NextPlot', 'add');
plot(cx, cy, 'b', 'Parent', AxesH);
axis equal
set(AxesH, 'XLim', [0, 100], 'YLim', [0, 100]);
7 个评论
Marwen Tarhouni
2017-2-16
编辑:Marwen Tarhouni
2017-2-16
@Marwen: Are you sure, that you use the code of GetPointsRandom from Answers: 142322? Here the complete code again with the bsxfun version for < R2016b:
function myTest
for nWant = [20, 200]
Dist = 6;
[X, Y] = GetPointsRandom(nWant, 100, 100, Dist);
alpha = linspace(0, 2*pi, 15).'; % 15 instead of 32, as you like
cx = bsxfun(@plus, X.', 0.5 * Dist * cos(alpha));
cy = bsxfun(@plus, Y.', 0.5 * Dist * sin(alpha));
figure; % Modified for nice XLimits in R2016b:
AxesH = axes('NextPlot', 'add');
plot(cx, cy, 'b', 'Parent', AxesH);
axis equal
set(AxesH, 'XLim', [0, 100], 'YLim', [0, 100]);
end
end
function [X, Y] = 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 avoid 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
end
I added a figure, because in your screenshot two axes are drawn on eachother. But this could not lead to the strange overlapping circles. The result on R2016b:

This differs substantially from you screenshot and I cannot explain why. Does your code differ from this one?
You see that 200 is near to the possible limit and then all methods to find such a dense random packing can fail. In the worst case an algorithm sets the points to a regular triangular grid with the double distance minus eps, such that the area is almost empty, but this very unlikely.
Please note, that this code allows a distance greater equal 6. If only greater 6 is wanted replace ">= dist_2" with "> dist_2" .
Marwen Tarhouni
2017-2-21
编辑:Marwen Tarhouni
2017-2-21
I completely agree with Marwen: the centre points have to be clearly indicated because each point is a mobile phone, the R circles tell each range (a fair approximation of how far each mobile phone may reach individually, am I right?), and the overlapping circles are visually helpful to verify no two points are closer than the required minimal distance.
Marwen merçi beacou und Viel Spaß
John BG
@Marwen: The center point of the circles are not displayed in my figures, because this detail has not been mentioned in this question. If you want to draw the circles with a radius of 15, use:
alpha = linspace(0, 2*pi, 32).';
R = 15;
cx = bsxfun(@plus, X.', R * cos(alpha));
cy = bsxfun(@plus, Y.', R * sin(alpha));
Add this to get the center points also:
plot(X, Y, '*');
You mention "'XLim', [-100, 100]" and "the area 100 * 100". The first is an area of 200*200.
"The maximum points that respect the 6 meter condition" is not uniquely defined, because this depends on the random locations. If you want the maximum number, and not a specified number, this might be a different problem. So I'm starting to get confused about what you need.
"Cell phones"? This detail has not been mentioned before and for the solution it does not matter.
John BG
2017-2-23
the centre points are not 'a detail' but the utmost important point, it's the first thing you should plot when considering visual presentation of anything attempting to answer this and similar questions.
John BG
Jan
2017-2-24
@John: The question was:
How we can draw circles of radius R on randomized points in
a area 100*100?
Then the center is "a detail [which] has not been mentioned".
KSSV
2017-2-14
0 个投票
2 个评论
Marwen Tarhouni
2017-2-14
KSSV
2017-2-14
That link....creates pints.....around those points circles can be drawn. Isn't it?
类别
在 帮助中心 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
