[問題] 用迴圈和pause製作動畫已回收
用while(1)寫了一個簡單的幾何動畫
三角星和或四角星持續旋轉 每隔0.5秒交換一次
(後來發現換圖後又從0度開始旋轉,先不管這個)
請問為什麼轉了幾回合後(大約交換了五六次)
整個速度會變慢?
不但越轉越慢,到最後還會把先前的圖疊加上去
這樣fill(x1,y1,'k')就顯得沒意義了
記憶體也越吃越大
code如下
x1=[50,50,-50,-50];y1=[-50,50,50,-50];
fill(x1,y1,'k');%背景
t=linspace(0,2*pi,500);
axis([-50,50,-50,50])
axis off
axis square
hold on
%三角星
x3=[0,7.071*cos(pi/6),40*cos(-pi/6),0,40*cos(7*pi/6),7.071*cos(5*pi/6)];
y3=[40,7.071*sin(pi/6),40*sin(-pi/6),-7.071,40*sin(7*pi/6),7.071*sin(5*pi/6)];
%四角星
x4=[40,5,0,-5,-40,-5,0,5];
y4=[0,5,40,5,0,-5,-40,-5];
trans = 0.5; %變形時間(秒)
rps = 0.3; %轉速(圈/秒)
step = 0.05; %畫格長度(秒)
theta = 2*pi*rps*step; %一個step轉的角度
A = [cos(theta), -sin(theta); sin(theta), cos(theta)]; %旋轉矩陣
while 1
%三角星
XY = [x3; y3];
for tt = 0 : step : trans
XY = A * XY;
fill(XY(1,:),XY(2,:),'r')
pause(step);
fill(x1,y1,'k')
end
%四角星
XY = [x4; y4];
for tt = 0 : step : trans
XY = A * XY;
fill(XY(1,:),XY(2,:),'r')
pause(step);
fill(x1,y1,'k')
end
end
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.4.235
推
12/07 16:49, , 1F
12/07 16:49, 1F
→
12/07 17:06, , 2F
12/07 17:06, 2F
→
12/08 10:00, , 3F
12/08 10:00, 3F