我一直在尝试找出如何制作动画来将这些点的形成完全一起移动到左侧(向负x轴方向),但是我没有任何想法。所以我的问题是,
是否可以制作将点的形成向左移动(所有点一起移动)的动画?如果是这样,请说明如何,或
还有另一种制作动画来显示所有点一起运动的方法吗?
this is the formation that I want to animate这是我用来构成的代码
谢谢
function ret = points(xL,yL,n) %n is the number of points in the formation, (xL,yL) denotes position of the leading point
x = [xL];
y = [yL];
r = 0.9;
xf = 0;
yf = 0;
teta = 34 * pi / 180;
i = 1;
while (i <= n),
if mod(i,2)==1,
xf = xf + (xL + (ceil(i/2)) * r * cos (teta));
yf = yf + (yL + (ceil(i/2)) * r * sin (teta));
elseif mod(i,2)==0,
xf = xf + (xL + (i/2) * r * cos (teta));
yf = yf + (yL - (i/2) * r * sin (teta));
endif
x = [x xf];
y = [y yf];
xf = 0;
yf = 0;
i++
endwhile
scatter(x,y)
endfunction
一旦计算出点的初始位置,您就可以简单地在for
循环中绘制它们,也就是将调用移动到scatter
循环中。
要向左移动点,您只需从x
组件中减去与动画每一步所需移动相对应的空间量。
要为动画设置for
循环,必须定义帧数和移动步长的大小;您可以将这两个参数添加为函数的输入
要实现动画效果,必须在每次迭代时使用pause
函数暂停绘图。
您还必须将x轴的下限至少固定到最左点的最后x位置。
可能的实现方式可能是:
function ret = points(xL,yL,n, n_frames, frame_step,enable_gif) %n is the number of points in the formation, (xL,yL) denotes position of the leading point x = [xL];
x = [xL];
y = [yL];
r = 0.9;
xf = 0;
yf = 0;
teta = 34 * pi / 180;
i = 1;
while (i <= n),
if mod(i,2)==1,
xf = xf + (xL + (ceil(i/2)) * r * cos (teta));
yf = yf + (yL + (ceil(i/2)) * r * sin (teta));
elseif mod(i,2)==0,
xf = xf + (xL + (i/2) * r * cos (teta));
yf = yf + (yL - (i/2) * r * sin (teta));
endif
x = [x xf];
y = [y yf];
xf = 0;
yf = 0;
i++
endwhile
% Open a figure
figure
% Add the axes to the figure
axes
% Get the limits of the x axis
x_min=min(x)
x_max=max(x)
% Define the number of frame for the animation
n_frames=13
% Define the step for the movement of the points
frame_step=1
% Loop over the number of frames
if(enable_gif)
create_gif('my_points_gif.gif',gcf,.3,0)
end
for i=1:n_frames
% lot the points
scatter(x-i,y)
% Adjust the limits of the x axis
xlim([x_min-frame_step*n_frames x_max])
if(enable_gif)
create_gif('my_points_gif.gif',gcf,.3,1)
else
% Pause the executiion
pause(.3)
end
end
endfunction
然后您可以如下调用该函数:
xL=1
yL=1
n=13
% Define the number of frame for the animation
n_frames=13
% Define the step for the movement of the points
frame_step=1
enable_gif=1
points(xL, yL,n,n_frames,frame_step,enable_gif)
创建动画gif的功能基于on-line documentation]中报告的信息
function create_gif(filename,fig,delay_time,write_mode)
%
% create_gif(filename,fig,delay_time,write_mode)
%
drawnow
frame = getframe(fig);
im = frame2im(frame);
[A,map] = rgb2ind(im,256);
if(write_mode == 0)
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',delay_time);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',delay_time);
end