Effacer les filtres
Effacer les filtres

Generate random circles in the square box with different diameters

6 vues (au cours des 30 derniers jours)
Laura
Laura le 13 Mar 2015
Commenté : TRAN Quang Vu le 22 Juil 2020
I was able to generate random circles inside the square box of dimension L=1 with the same diameter without overlapping, where X and Y are between -0.5 to 0.5.
Now, I want to generate random circles with 3 different diameters and I need to save the location of X,Y and diameter for each circle. It would be nice if we have the same number of circles per each diameter.
If you have any idea how to do so, please help me. Thanks
  1 commentaire
TRAN Quang Vu
TRAN Quang Vu le 22 Juil 2020
Hello,
I want to generate random circles inside the square box of dimension L=1 with the same diameter without overlapping, where X and Y are between -0.5 to 0.5. Can you help me? Thansk you.

Connectez-vous pour commenter.

Réponse acceptée

Brendan Hamm
Brendan Hamm le 13 Mar 2015
If you have k circles, then you can create three different areas (and thus different diameters) using:
k = 30;
X = rand(k,1) - 0.5;
Y = rand(k,1) - 0.5;
a = randi([1 3],k,1); %Random integers from discrete U([1,3])
s = scatter(X,Y,a);
If you want the same number of each diameter:
n = k/3; % May need to take floors if 3 is not a divisor of k
a = [5*ones(n,1); 10*ones(n,1); 15*ones(n,1)]; % Equal number of fives, tens, and fifteens
a = a(randperm(k)); % Shuffle the vector
s = scatter(X,Y,a);
randperm(k) permutes the integers 1:k, so I use this to randomly shuffle the values, although arguably if you were to specify the first ten observations were to be labeled size 5, and the next ten size 10, ... the (x,y) values are still random and there is no real need to do the permutation.
  2 commentaires
DINESH CHANDRA
DINESH CHANDRA le 16 Août 2016
Modifié(e) : Image Analyst le 16 Août 2016
Is it possible that circles overlapping prior circles can be discarded?
Image Analyst
Image Analyst le 16 Août 2016
Of course. Just keep track of radii and centers. If the distance from the center of the next/proposed circle is within the sum of the two radii, then just don't add it. If x and y are arrays of the centers, then do something like:
% Get distances of (xNew, yNew) from all other circles
distances = sqrt((xNew-x).^2+(yNew-y).^2);
% Get sums of radii
radiiSums = radiusNew + r;
% See if we can add this new one
okToAdd = all(distances > radiiSums);
if okToAdd
x(end+1) = xNew;
y(end+1) = yNew;
r(end+1) = radiusNew;
end

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 13 Mar 2015
See my attached demo. Adapt it as needed with the rand() function to make random radii.

Community Treasure Hunt

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

Start Hunting!

Translated by