[英]how to move circle using plot in Matlab
我想画一个圆并在Matlab绘图图中移动它。 我正在使用
viscircles([8.1, 8.5], 1);
画圆圈。 如何再次调用它以绘制一个新圆并删除原始圆? 还有一种方法可以使用
drawnow
功能做到这一点?
无需删除和重绘,只需在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
一种可能性是创建自己的圆形函数,该函数将返回图柄:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.