【问题标题】:How to animate rectangles如何为矩形设置动画
【发布时间】:2017-05-22 10:27:56
【问题描述】:

我正在尝试使用 Matlab2013 中的矩形函数获得的圆圈制作动画。为了使情节动起来,我尝试使用clfdrawnowpause,但似乎不起作用。另一方面,当我使用点或线时,我使用 setpause 并且效果很好,但我看不到将它们与矩形一起使用的方法。

在这里,我向您展示了我是如何尝试使用drawnow 做到这一点的。有1000个时间步长,每个时间步长我都存储了四个圆的xy坐标。​​

%At every time step I would like to plot 4 circles. 
PosxProt = rand(1000, 4)
PosyProt = rand(1000, 4)

for i=1:1000
    clf
    hold on
    for j=1:4
        rP=0.345; %radius of the circles
        cP=[PosxProt(i,j) PosyProt(i,j)]; %center of the circles
        rectangle('Position',[cP-rP 2*rP 2*rP],'Curvature',[1 1],'facecolor','r') %plot circle
    end
    drawnow
    pause(0.05)

end

【问题讨论】:

  • 什么不起作用?
  • 我的意思是动画不起作用。
  • 对我来说,动画确实有效。当您在循环中放置断点或延长暂停持续时间时,它是否有效?
  • 我刚刚意识到它确实有效。我现在已经确定了我的真实代码的问题,那是另一个问题。非常感谢!

标签: matlab animation plot rectangles drawnow


【解决方案1】:

您可以使用以下等式对矩形进行参数化:

 % 2*p and 2*q are the size of the rectangle
 t = 0:0.01:1;
 x=p*(abs(cos(t))*cos(t)+abs(sin(t))*sin(t))
 y=q*(abs(cos(t))*cos(t)-abs(sin(t))*sin(t))

然后使用彗星绘制矩形:

 comet(x,y)

您还可以为comet here 找到更多选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-12
    • 2019-07-11
    • 1970-01-01
    • 2021-02-15
    • 2013-07-19
    • 1970-01-01
    • 2019-05-16
    • 2014-08-26
    相关资源
    最近更新 更多