matlab绘制雪花飘落动图
首先是绘制静态的雪花,程序如下, 参考网页(https://blog.csdn.net/slandarer/article/details/108873988)
function drawSnow(x,y,len,angle,width)
for theta=0:pi/3:2*pi-pi/3
xT=cos(theta+angle)*len+x;
yT=sin(theta+angle)*len+y;
plot([x,xT],[y,yT],'color',[0 134 207]./255,'linewidth',width);hold on
for br=[0.4 0.55 0.7]
BX=x+cos(theta+angle)*len*br;
BY=y+sin(theta+angle)*len*br;
LX=BX+cos(theta+angle+pi/3).*len.*0.4;
LY=BY+sin(theta+angle+pi/3).*len.*0.4;
RX=BX+cos(theta+angle-pi/3).*len.*0.4;
RY=BY+sin(theta+angle-pi/3).*len.*0.4;
plot([BX,LX],[BY,LY],'color',[0 134 207]./255,'linewidth',width.*0.8);
plot([BX,RX],[BY,RY],'color',[0 134 207]./255,'linewidth',width.*0.8);
xlim([0 500]);ylim([0 500]);
end
end
end
然后是绘制动图并制作视频:
mainfig=figure('units','pixels','position',[300 80 1200 1200],...
'Numbertitle','off','menubar','none','resize','off','name','falling snow');
axes('parent',mainfig,'position',[0 0 1 1],...
'xlim',[0 500],...
'ylim',[0 500],...
'layer','bottom',...
'visible','on',...
'color',[255 255 255]./255,...
'Xtick',[],'Ytick',[]);
hold on
vidObj = VideoWriter('snow.avi');
vidObj.FrameRate = 1;
open(vidObj);
num=15;
x=rand(num,1)*400+50;y=rand(num,1)*100+400;len=repmat(10,num,1);angle=repmat(2*pi,num,1);width=repmat(0.3,num,1);
speed=rand(num,1)*9.5+10;
for i=1:20
for j=1:num
drawSnow(x(j),y(j)-i*speed(j),len(j)/2,angle(j)+i*2,width(j))
end
f=getframe(gcf);
writeVideo(vidObj,f);
pause(0.8)
cla;
end
close(vidObj);
效果图:

动态视频见我的B站