MATLAB新年烟花祝MATLABer新年快乐
Posted slandarer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB新年烟花祝MATLABer新年快乐相关的知识,希望对你有一定的参考价值。
2023年就要到了!!
画个简简单单的烟花~
我保证再写几期好玩的就开始写建模算法啥的,毕竟美赛也快到了。
代码依旧是各种矩阵运算,建议17b之后版本使用,首先看一眼烟花效果哈:
随机颜色随机数量烟花,另外还添加了系统自带的音效,如果觉得不好听可以扔掉或者换点其他音效。
绘图原理
渐变的烟花是使用fill
函数绘制的,只需要将最后一个数值设置为nan
就能由面变成线:
x=linspace(1,10,30);
y=sin(x);
y(end)=NaN;
fill(x,y,y,'EdgeColor','interp','LineWidth',10);
完整代码
function fireWorks2023
% @author : slandarer
% 坐标区域初始化及修饰
ax=gca;hold on;fig=gcf;
set(ax,'XLim',[0,20],'YLim',[0,20],'PlotBoxAspectRatio',[1,1,1],...
'XColor','none','YColor','none','Color',[0,0,0],'Tag',char([100,105,115,112,...
40,39,20316,32773,58,115,108,97,110,100,97,114,101,114,39,41]));eval(ax.Tag)
fig.Position(1:2)=fig.Position(1:2)/5;
fig.Position(3:4)=fig.Position(3:4).*2;
% 基础数据
baseNum=100;
interpNum=15;
fwSound=load('splat.mat');
% 绘制星星
scatter(rand(1,50)*20,rand(1,50).*10+10,4,'white','filled','MarkerEdgeColor','none','MarkerFaceAlpha',.6)
% 循环烟花升空绽放过程
while true
randFW(baseNum,interpNum,rand(3,3)./2+.5,randi(3))
delete(findobj('type','patch'))
pause(.5)
end
function randFW(num1,num2,C,N)
randC=rand(N,2).*[20,10]+[0,8];
% 绘制升空过程
scHdl=scatter(randC(:,1),randC(:,2).*0,25,'filled','CData',C(1:N,:));
sound(fwSound.y(1:7200),fwSound.Fs/1.2)
for k=0:.02:1
pause(.02)
scHdl.YData=randC(:,2).*k;
drawnow;
end
delete(scHdl)
% 随机生成数据并绘制渐变烟花
randXSet=zeros(num2,num1,N);
randYSet=zeros(num2,num1,N);
for n=1:N
randTheta=rand(1,num1).*2.*pi;
randR=rand(1,num1).*3+2;
randX=cos(randTheta).*randR;
randY=sin(randTheta).*randR;
randX=randX.*linspace(0,1,num2)';
randY=randY.*linspace(0,1,num2)';
randX(end,:)=nan;
randY(end,:)=nan;
randXSet(:,:,n)=randX;
randYSet(:,:,n)=randY;
end
for n=1:N
for i=1:num1
fwHdl(n,i)=fill(randXSet(:,i,n)+randC(n,1),randYSet(:,i,n)+randC(n,2),...
linspace(0,1,num2),'EdgeColor','interp',...
'LineWidth',2,'EdgeAlpha',.9,'FaceVertexCData',C(n,:).*linspace(0,1,num2)');
end
end
sound(fwSound.y(7201:10001),fwSound.Fs)
% 烟花绽放过程
for k=.1:.05:1
for n=1:N
for i=1:num1
fwHdl(n,i).XData=randXSet(:,i,n).*k+randC(n,1);
fwHdl(n,i).YData=randYSet(:,i,n).*k+randC(n,2);
end
end
pause(.001)
drawnow;
end
end
end
考虑重力版
function fireWorks2023_g
% @author : slandarer
% 坐标区域初始化及修饰
ax=gca;hold on;fig=gcf;
set(ax,'XLim',[0,20],'YLim',[0,20],'PlotBoxAspectRatio',[1,1,1],...
'XColor','none','YColor','none','Color',[0,0,0],'Tag',char([100,105,115,112,...
40,39,20316,32773,58,115,108,97,110,100,97,114,101,114,39,41]));eval(ax.Tag)
fig.Position(1:2)=fig.Position(1:2)/5;
fig.Position(3:4)=fig.Position(3:4).*2;
% 基础数据
baseNum=100;
interpNum=15;
fwSound=load('splat.mat');
% 绘制星星
scatter(rand(1,50)*20,rand(1,50).*10+10,4,'white','filled','MarkerEdgeColor','none','MarkerFaceAlpha',.6)
% 循环烟花升空绽放过程
while true
randFW(baseNum,interpNum,rand(3,3)./2+.5,randi(3))
delete(findobj('type','patch'))
pause(.5)
end
function randFW(num1,num2,C,N)
randC=rand(N,2).*[20,10]+[0,8];
% 绘制升空过程
scHdl=scatter(randC(:,1),randC(:,2).*0,25,'filled','CData',C(1:N,:));
sound(fwSound.y(1:7200),fwSound.Fs/1.2)
for k=0:.02:1
pause(.02)
scHdl.YData=randC(:,2).*k;
drawnow;
end
delete(scHdl)
% 随机生成数据并绘制渐变烟花
randXSet=zeros(num2,num1,N);
randYSet=zeros(num2,num1,N);
for n=1:N
randTheta=rand(1,num1).*2.*pi;
randR=rand(1,num1).*3+2;
randX=cos(randTheta).*randR;
randY=sin(randTheta).*randR;
randX=randX.*linspace(0,1,num2)';
randY=randY.*linspace(0,1,num2)';
randX(end,:)=nan;
randY(end,:)=nan;
randXSet(:,:,n)=randX;
randYSet(:,:,n)=randY;
end
for n=1:N
for i=1:num1
fwHdl(n,i)=fill(randXSet(:,i,n)+randC(n,1),randYSet(:,i,n)+randC(n,2),...
linspace(0,1,num2),'EdgeColor','interp',...
'LineWidth',2,'EdgeAlpha',.9,'FaceVertexCData',C(n,:).*linspace(0,1,num2)');
end
end
sound(fwSound.y(7201:10001),fwSound.Fs)
% 烟花绽放过程
for k=.1:.05:1
for n=1:N
for i=1:num1
fwHdl(n,i).XData=randXSet(:,i,n).*k+randC(n,1);
R2=(randXSet(:,i,n).*k).^2+(randYSet(:,i,n).*k).^2;
fwHdl(n,i).YData=randYSet(:,i,n).*k+randC(n,2)-R2./15;
end
end
pause(.001)
drawnow;
end
end
end
电脑比较好的可以试试这个3D版
function FireWorks3D
% @author : slandarer
ax=gca;hold on;grid on;view(-55,12)
ax.XLim=[0,20];
ax.YLim=[0,20];
ax.ZLim=[0,20];
ax.DataAspectRatio=[1,1,1];
ax.Projection='perspective';
ax.LooseInset=[0,0,0,0];
ax.Color=[0,0,0];
ax.Position=[-1/6,-1/6,1+1/3,1+1/3];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';
fig=gcf;
fig.Position(1:2)=fig.Position(1:2)/5;
fig.Position(3:4)=fig.Position(3:4).*2;
fig.Color=[0,0,0];
[X,Y]=meshgrid(linspace(0,20,25));
scatter3(X(:),Y(:),X(:).*0+rand(length(X(:)),1)./10,前端:运用canvas标签绘制烟花,提前祝大家新年快乐