Borrar filtros
Borrar filtros

How to plot a number of circles with the same radius, but the position of the circles is randomly put

5 visualizaciones (últimos 30 días)
I need to plot a number of circles with the same radius. Number of circles and the radius are input parameters. Also circles are randomly positioned. For example: The program ask user to input the number of the circles, then ask to input the radius of the circle. Here, the radius is the same for all circles. Then by using input parameters program randomly put the circles.
  3 comentarios
Walter Roberson
Walter Roberson el 24 de Jun. de 2016
Beibit Sautbek comments,
Circles may not overlap. It should separately located

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 23 de Jun. de 2016
N = 10 ; % number of circles
r = 0.25 ; % radius of circel
%
C = rand(N,2) ;
%
th = linspace(0,2*pi) ;
x = r*cos(th) ;
y = r*sin(th) ;
% Loop for each circle
for i = 1:N
xc = C(i,1)+x ;
yc = C(i,2)+y ;
hold on
plot(xc,yc) ;
end
axis equal
  1 comentario
Beibit Sautbek
Beibit Sautbek el 23 de Jun. de 2016
Dear Dr. Siva Srinivas Kolukula, The codes is great, thank you very much. However the circles are overlap each other, I need separately located circles. Maybe I need the certain distance between the circles Thank you very much

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 16 de Jul. de 2016
I know you already figured out how to adapt Siva's solution to prevent overlap, because you accepted it, but in case anyone else wants to know how to make a set of non-overlapping circles, see the code below:
numCircles = 1;
numCirclesMax = 50;
radius = 0.05;
maxIterations = 100 * numCirclesMax; % Fail Safe.
iteration = 1;
while numCircles < numCirclesMax && iteration < maxIterations
xTrial = rand;
yTrial = rand;
iteration = iteration + 1; % Fail safe.
% Only need to check for overlap for second and later circles.
if numCircles > 1
% Find distance from other, prior circles.
distances = sqrt((xTrial - x) .^ 2 + (yTrial - y) .^ 2);
if min(distances) < 2 * radius
% It's overlapping at least one of the prior ones
continue; % Skip to end of loop and continue with loop.
end
end
x(numCircles) = xTrial;
y(numCircles) = yTrial;
numCircles = numCircles + 1;
end
radii = radius * ones(1, length(x));
centers = [x', y'];
viscircles(centers, radii);
grid on;
axis equal
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Walter Roberson
Walter Roberson el 23 de Jun. de 2016
You might be able to use viscircles() if you have the Image Processing toolbox
  2 comentarios
jahanzaib ahmad
jahanzaib ahmad el 21 de Nov. de 2018
Editada: jahanzaib ahmad el 21 de Nov. de 2018
using the same code i m trying to generate circle of different radius . @is it possible ?
Image Analyst
Image Analyst el 21 de Nov. de 2018
Well, almost the same. Of course you need to generate the random radii. See attached m-file that makes the figure below:
0000 Screenshot.png

Iniciar sesión para comentar.


jahanzaib ahmad
jahanzaib ahmad el 21 de Nov. de 2018
ONE LAST THING CAN U FIX THEM IN SEMI CIRCLE .. ?
axis([-1.1 1.1 -0.1 1.1]);

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by