MATLAB程序演示烟花燃放过程烟花(礼花)的朵朵绽放给夜空带来炫彩,瞬间绚丽至极,迸射出璀璨夺目的光彩。
只是还来不及在脑海中印上花魂,她已昙花一现般,烟花(礼花)朵朵绽放,瞬间绚丽至极,迸射出璀璨夺目的光彩。
只是还来不及在脑海中印上花魂,她已昙花一现般的消逝了。
多少人痴迷于烟花的美,痴迷于她飘忽不定的幻影。
利用MATLAB程序演示烟花的美,虽然她不如现实那样逼真,但一样会吸引人。
附源程序:function firework% 烟花烟花满天飞% CopyRight:xiezhh(谢中华)% 2011.6.25OldHandle = findobj( 'Type', 'figure', 'Tag', 'FireWork' ) ;if ishandle(OldHandle)close(OldHandle) ;end% 图形窗口初始化fig = figure('units','normalized','position',[0.1 0.1 0.8 0.8],...'menubar','none','name','烟花满天飞(谢中华制作)',...'numbertitle','off','color',[0 0 0],'tag','FireWork');% 烟花炸开前的初始位置h0 = line(0,0,0,'linestyle', 'none',...'marker','.',...'markersize',18,...'MarkerEdgeColor',[1 1 1],...'MarkerFaceColor',[1 1 1],...'EraseMode' , 'xor');% 设置坐标系显示属性axis equalaxis([-50 50 -50 50 0 100])axis offview(-42,22)% 设置参数rate = 1:-0.01:0; % 颜色衰减率v0 = 45; % 烟花头初始速度g = -9.8; % 重力加速度usedtime = -v0/g; % 烟花头爆炸前所经历时间zs = v0*usedtime+0.5*g*usedtime^2; %烟花头爆炸前达到的最高高度theta = rand(250,1)*2*pi; % 各粒子速度的方位角phi = rand(250,1)*2*pi-pi; % 各粒子速度的仰角age = 20; % 粒子生存期% 常用颜色矩阵colormat = [1.0 0.5 0.51.0 0.75 0.51.0 1.0 0.50.75 1.0 0.50.5 1.0 0.50.5 1.0 0.750.5 1.0 1.00.5 0.75 1.00.5 0.5 1.00.75 0.5 1.01.0 0.5 1.01.0 0.5 0.75];% 随机产生各粒子对应的颜色序号colorid = randsample(12,250,true);% 粒子对应的颜色矩阵colormat = colormat(colorid,:);% 粒子颜色与背景色(夜色)的距离colordist = sqrt(sum(colormat.^2,2));v1 = 20; % 粒子的初始速度k = 1; % 颜色衰减率初始序号timerA = timer('TimerFcn',@TimerFcnA,...'executionmode','fixedspacing','Period',0.001);start(timerA);h = getappdata(gcf,'HandleParticle');timerB = timer('TimerFcn',{@TimerFcnB},...'executionmode','fixedspacing','period',0.001);%-------------------------------------------------------------------------- % 定时器回调函数(烟花头)%-------------------------------------------------------------------------- function TimerFcnA(timerA,event)ta = get(timerA,'TasksExecuted')*0.1;if ta <= usedtimez = v0*ta+0.5*g*ta^2;set(h0,'zdata',z,...'MarkerEdgeColor',[1 1 1],...'MarkerFaceColor',[1 1 1])drawnow%pause(0.01)elsedelete(h0)stop(timerA);x0 = zeros(2,250);y0 = zeros(2,250);z0 = zs*ones(2,250);h = line(x0,y0,z0,'linestyle', 'none',...'marker','h',...'markersize',12,...'MarkerEdgeColor',[1 1 1],...'MarkerFaceColor',[1 1 1],...'EraseMode' , 'xor');setappdata(gcf,'HandleParticle',h);start(timerB);endend%-------------------------------------------------------------------------- % 定时器回调函数(粒子)%-------------------------------------------------------------------------- function TimerFcnB(timerB,event)tb = get(timerB,'TasksExecuted')*0.15;if age>0 && any(colordist>=0.05)colormat = colormat*rate(k);colordist = sqrt(sum(colormat.^2,2));for i = 1:250xi = v1*cos(phi(i))*cos(theta(i))*tb;yi = v1*cos(phi(i))*sin(theta(i))*tb;zi = zs+v1*sin(phi(i))*tb+0.5*g*tb^2;set(h(i),'XData',xi,'YData',yi,'ZData',zi,...'MarkerEdgeColor',colormat(i,:),...'MarkerFaceColor',colormat(i,:))enddrawnowage = age-0.1;k = k+1;elsestop(timerB);delete(h)endend%-------------------end。