I need to plot circles with different radius and centers .Also I need to extract that information of centers and radius.

3 views (last 30 days)
I am trying to produce circles of differents radius within a range and differents centers. I could make it to produce the radius but not the centers and I cannot make the plot. It showed "Matrix dimensions must agree". This is the code, I will appreciate any help you can provide me:
N=10;
R= randi ([5 20],1,N);
xs = [0 5 5 0 0] ; ys = [0 0 5 5 0] ;
th = linspace(0,2.*pi) ;
xc = R.*cos(th) ; yc = R.*sin(th) ;
x = R:2*R:S-R ;
y = R:2*R:S-R ;
[X,Y] = meshgrid(x,y) ;
% plot
figure
hold on
plot(xs,ys,'r') ;
plot(X,Y,'.b')
%
for i = 1:size(X,1)
for j = 1:size(X,2)
plot(X(i,j)+xc,Y(i,j)+yc,'b')
end
end
title(sprintf('Number of circles = %d',numel(X)))

Answers (2)

Mathieu NOE
Mathieu NOE on 26 Jan 2023
hello
no need to use meshgrid here (why ??)
try this instead
the centers and circles are plotted with same color here (but different color at each iteration)
N=10;
R= randi ([5 20],1,N); % radii
xc = randi ([-5 10],1,N); % x centers
yc = randi ([8 15],1,N); % y centers
th = linspace(0,2.*pi) ;
% plot
CM = jet(N); % See the help for COLORMAP to see other choices.
figure
hold on
for ck = 1:N
xcircle = xc(ck) + R(ck)*cos(th) ;
ycircle = yc(ck) + R(ck)*sin(th) ;
% a=rand(1,3); % color triplet value , so below the center and the circle are plotted with same color (but different at each iteration)
a=CM(ck,:); % color triplet value , so below the center and the circle are plotted with same color (but different at each iteration)
plot(xc(ck),yc(ck),'Marker','*','color',a,'DisplayName',num2str(ck));
plot(xcircle,ycircle,'color',a,'DisplayName',num2str(ck));
end
axis equal
title(sprintf('Number of circles = %d',N))

Image Analyst
Image Analyst on 26 Jan 2023
Try this. Adapt as needed.
N = 10;
% Define circle parameters.
radii = 10 * rand(1, N);
x = 10 * rand(1, N);
y = 10 * rand(1, N);
% Plot centers
plot(x, y, 'r+');
% Plot circles
viscircles([x(:), y(:)], radii);
axis equal
grid on;

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!