在 Matlab 中为沿 X 轴滑动的正方形设置动画
Animate a Square that is sliding along X axis in Matlab
我有一个正方形,它根据列向量 x(在底部)沿 x 轴来回移动。
我想为正方形沿 x 轴来回滑动的运动设置动画。我尝试在 for 循环中使用 patch、hold on、hold off 和 Get Frame 函数,但这没有用。相反,结果是正方形被弄脏了,因为旧正方形没有从图中移除。这是代码:
clf
s = .1;
figure(1);
axis equal;
axis([-1 1 -1 1]);
for i = 1:length(x)
hold on
X = [x(i,1) - .5*s;
x(i,1) + .5*s;
x(i,1) + .5*s;
x(i,1) - .5*s;];
Y = [-.5*s;
-.5*s;
+.5*s;
+.5*s];
patch(X,Y,'k');
hold off;
h = gca;
F(i) = getframe;
end
movie(F);
这里是列向量x:
0.250000000000000
0.230782159143542
0.176280043536689
0.0953146856798128
0.000650212865865713
-0.0932030485077490
-0.172186726125664
-0.224543041359108
-0.242303458811820
-0.222203182817347
-0.166192085851484
-0.0817507793230268
0.0186298767049816
0.118170991653033
0.198905540101543
0.246001260876051
0.250959024174034
0.213107724416597
0.138822733533125
0.0407394702062158
-0.0642948796656877
-0.158085053404849
-0.224759025665608
-0.253100286376047
-0.238297378581389
-0.182618266275528
-0.0958896904713370
0.00602839522197084
0.104668511135333
0.183752954307020
0.231574999618735
0.242127812051811
0.214632538642815
0.153856203337548
0.0689710768423088
-0.0273378670448299
-0.120371418450032
-0.195696352800403
-0.241063326708591
-0.249170206446669
-0.218574858836030
-0.154523974516009
-0.0674660734309435
0.0288890782751109
0.119813330828138
0.191877626928792
0.234364270672030
0.240941167981351
0.209893379667572
0.145130717726666
0.0553977698549078
-0.0457864898288502
-0.141584098341353
-0.214945647396668
-0.252189311075765
-0.246607701950267
-0.199000344173733
-0.117539948553725
-0.0163532898953576
0.0870695406469115
0.175127325624604
0.233430214463870
0.252410945754133
0.228805022755430
0.165868916165404
0.0741157391779560
-0.0299825244149044
-0.127575941311610
-0.201938733706939
-0.241944848784945
-0.242264519853537
-0.203786790215477
-0.132560148710489
-0.0395394071796979
0.0608871776036858
0.152636629298216
0.220295640189107
0.252442054488179
0.243446202365058
0.195322881013323
0.116647868987791
0.0209866982124944
-0.0762128632358914
-0.160101037237629
-0.218549398091714
-0.243311095720934
-0.230961374436814
-0.183209095455776
-0.106644747551930
-0.0123109118042467
0.0857290198345198
0.171900620989060
0.231541484378119
0.254192056475618
0.235371165049059
0.178352335891224
0.0928137722294060
-0.00672172862573606
-0.104037530998919
-0.183858736347838
我最终放弃了补丁功能,因为它似乎不适用于 hold on hold off 命令。我使用 plot 函数来绘制四条线,如下所示。
clc
clf
s = .25;
min = -2;
max = 2;
for i = 1:length(x)
X = [x(i,1)-.5*s, x(i,1)+.5*s];
Y = [-.5*s, -.5*s];
plot(X,Y,'b');
axis equal
axis([min max min max]);
hold on;
X = [x(i,1)+.5*s, x(i,1)+.5*s];
Y = [-.5*s, .5*s];
plot(X,Y,'b');
axis equal
axis([min max min max]);
hold on;
X = [x(i,1)+.5*s, x(i,1)-.5*s];
Y = [.5*s, .5*s];
plot(X,Y,'b');
axis equal;
axis([min max min max]);
hold on;
X = [x(i,1)-.5*s, x(i,1)-.5*s];
Y = [.5*s, -.5*s];
plot(X,Y,'b');
axis equal;
axis([min max min max]);
hold off
F(i) = getframe;
end
movie(F)
我有一个正方形,它根据列向量 x(在底部)沿 x 轴来回移动。 我想为正方形沿 x 轴来回滑动的运动设置动画。我尝试在 for 循环中使用 patch、hold on、hold off 和 Get Frame 函数,但这没有用。相反,结果是正方形被弄脏了,因为旧正方形没有从图中移除。这是代码:
clf
s = .1;
figure(1);
axis equal;
axis([-1 1 -1 1]);
for i = 1:length(x)
hold on
X = [x(i,1) - .5*s;
x(i,1) + .5*s;
x(i,1) + .5*s;
x(i,1) - .5*s;];
Y = [-.5*s;
-.5*s;
+.5*s;
+.5*s];
patch(X,Y,'k');
hold off;
h = gca;
F(i) = getframe;
end
movie(F);
这里是列向量x:
0.250000000000000 0.230782159143542 0.176280043536689 0.0953146856798128 0.000650212865865713 -0.0932030485077490 -0.172186726125664 -0.224543041359108 -0.242303458811820 -0.222203182817347 -0.166192085851484 -0.0817507793230268 0.0186298767049816 0.118170991653033 0.198905540101543 0.246001260876051 0.250959024174034 0.213107724416597 0.138822733533125 0.0407394702062158 -0.0642948796656877 -0.158085053404849 -0.224759025665608 -0.253100286376047 -0.238297378581389 -0.182618266275528 -0.0958896904713370 0.00602839522197084 0.104668511135333 0.183752954307020 0.231574999618735 0.242127812051811 0.214632538642815 0.153856203337548 0.0689710768423088 -0.0273378670448299 -0.120371418450032 -0.195696352800403 -0.241063326708591 -0.249170206446669 -0.218574858836030 -0.154523974516009 -0.0674660734309435 0.0288890782751109 0.119813330828138 0.191877626928792 0.234364270672030 0.240941167981351 0.209893379667572 0.145130717726666 0.0553977698549078 -0.0457864898288502 -0.141584098341353 -0.214945647396668 -0.252189311075765 -0.246607701950267 -0.199000344173733 -0.117539948553725 -0.0163532898953576 0.0870695406469115 0.175127325624604 0.233430214463870 0.252410945754133 0.228805022755430 0.165868916165404 0.0741157391779560 -0.0299825244149044 -0.127575941311610 -0.201938733706939 -0.241944848784945 -0.242264519853537 -0.203786790215477 -0.132560148710489 -0.0395394071796979 0.0608871776036858 0.152636629298216 0.220295640189107 0.252442054488179 0.243446202365058 0.195322881013323 0.116647868987791 0.0209866982124944 -0.0762128632358914 -0.160101037237629 -0.218549398091714 -0.243311095720934 -0.230961374436814 -0.183209095455776 -0.106644747551930 -0.0123109118042467 0.0857290198345198 0.171900620989060 0.231541484378119 0.254192056475618 0.235371165049059 0.178352335891224 0.0928137722294060 -0.00672172862573606 -0.104037530998919 -0.183858736347838
我最终放弃了补丁功能,因为它似乎不适用于 hold on hold off 命令。我使用 plot 函数来绘制四条线,如下所示。
clc
clf
s = .25;
min = -2;
max = 2;
for i = 1:length(x)
X = [x(i,1)-.5*s, x(i,1)+.5*s];
Y = [-.5*s, -.5*s];
plot(X,Y,'b');
axis equal
axis([min max min max]);
hold on;
X = [x(i,1)+.5*s, x(i,1)+.5*s];
Y = [-.5*s, .5*s];
plot(X,Y,'b');
axis equal
axis([min max min max]);
hold on;
X = [x(i,1)+.5*s, x(i,1)-.5*s];
Y = [.5*s, .5*s];
plot(X,Y,'b');
axis equal;
axis([min max min max]);
hold on;
X = [x(i,1)-.5*s, x(i,1)-.5*s];
Y = [.5*s, -.5*s];
plot(X,Y,'b');
axis equal;
axis([min max min max]);
hold off
F(i) = getframe;
end
movie(F)