【问题标题】:how to move circle using plot in Matlab如何在Matlab中使用绘图移动圆圈
【发布时间】:2017-10-17 07:17:32
【问题描述】:

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

 viscircles([8.1, 8.5], 1);

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

drawnow

这个功能?

【问题讨论】:

  • 你需要使用viscircles还是可以用不同的方式画一个圆圈?
  • 是的,但如果可能的话,最好使用 viscircles

标签: matlab matlab-figure


【解决方案1】:

无需删除和重绘,只需通过在 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

【讨论】:

  • 这似乎工作得很好,但是当我们不调用 plot 或drawow 时,plot 怎么知道画圆呢?
  • 重点是只画一次圆(第6行),然后移动x和y坐标。没有重绘/重绘。您可以将pause 替换为drawnow。我使用pause 以更延迟的方式可视化更改
【解决方案2】:

一种可能性是创建您自己的循环函数,该函数返回绘图句柄:

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);

【讨论】:

  • 谢谢。这似乎在移动圆圈。但是你有没有机会知道如何修复一个情节,以便我可以看到圆圈在移动?现在图表随着圆圈缩放,所以我不能完全判断圆圈是否在移动。
  • 您可以在第一次调用之前修复轴:例如:'figure(1);axis([-10 10 -10 10]); h = my_circle(2,1,4); ... `
猜你喜欢
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多