如何在 Matlab 中使用绘图移动圆圈

how to move circle using plot in Matlab

我想画一个圆并在 Matlab 绘图中移动它。 我正在使用

 viscircles([8.1, 8.5], 1);

画圆圈。我如何再次调用它来绘制一个新的圆圈并删除原来的圆圈?还有一种方法可以使用

drawnow

有这个功能吗?

一种可能是创建自己的圆函数,returns 绘图句柄:

function h = my_circle(xc,yc,r)
% xc x-center of circle
% yc y-center of circle
% r radius

th = 0:pi/50:2*pi;
x = r * cos(th) + xc;
y = r * sin(th) + yc;
hold on
h = plot(x, y);
hold off;

有了这个你就可以画出你的圆了

h = my_circle(1,2,3);

如果您不再需要它,请将其删除:

delete(h)

之后你可以绘制一个新的:

h2 = my_circle(1,2,4);

无需删除并重新绘制,只需在 X 和 Y 数据中引入一些常量即可移动圆。

%%%%Borrowing some code from irreducible's answer%%%%
xc=1; yc=2; r=3;
th = 0:pi/50:2*pi;
x = r * cos(th) + xc;
y = r * sin(th) + yc;
h = plot(x, y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis([-20 20 -20 20]); %Fixing axis limits 
for k=1:20       %A loop to visualise new shifted circles
    h.XData = x + randi([-10 10],1);   %Adding a constant in the x data
    h.YData = y + randi([-10 10],1);   %Adding a constant in the y data
    pause(0.5);  %Pausing for some time just for convenient visualisation
end