- /
-
Random fireworks
on 19 Nov 2023
- 8
- 101
- 0
- 1
- 572
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent x y vx vy c p1 markerSize vDrag weightFactor shrinkFactor;
if f==1
nSparks = 150; % number of sparks
markerSize = 14; % size of sparks
vScale = 10; % velocity scale
vDrag = 0.65; % a bit of drag so sparks don't travel too far
weightFactor = 0.07; % what goes up...
shrinkFactor = 0.8; % sparks loose intensity with time
x = ones(1,nSparks)*randn(1)*100; % position of explosion
y = ones(1,nSparks)*randn(1)*100+150;
vx = randn(1,nSparks)*vScale; % velocity of each spark
vy = randn(1,nSparks)*vScale;
c = 0.3+0.7*rand(1,3);
p1 = plot(x,y,'o','MarkerSize',markerSize,'MarkerEdgeColor','none', ...
'MarkerFaceColor',c); % new explosion, plots all sparks
axis([-400 400 -400 400]);
set(gca,'Color','k');
else
set(p1,'xdata',x+(f^vDrag)*vx, ...
'ydata',y+(f^vDrag)*vy-weightFactor*(f)^2, ...
'MarkerSize',markerSize/(f^shrinkFactor));
end
end