- /
 - 
        
Celebrations in the MATropolis!
 
        on 13 Nov 2023
        
        
 
    - 23
 - 187
 - 0
 - 4
 - 1691
 
drawframe(1);
 Write your drawframe function below
function drawframe(f)
persistent handles
if f == 1 || isempty(handles)
    rng('default')
    axes(colormap=gray,View=[172,22],Color='k')
    hold on
    camva(3)
    h=randg(1,25);          % building heights
    b=bar3(h);
    axis equal
    g=.17;                  % height and width of windows
    [m,k]=ndgrid(1:25);     % (x,y) building indices
    handles.w = gobjects(0);
    for i=1:625             % loop through each building
        % Interpolate building color according to building height
        % Ideally this should be outside the loop but that would require more characters
        q=b(m(i));
        set(q,'CData',q.ZData,'FaceColor','interp')
        for j=1:2                   % loop through each column of windows
            L=k(i)-.27+(j-1)*.37;   % in k-a+(j-1)*b, a is the signed distance from center of building in row k(i) to left edge of window in column j and b is the interval between left edges of windows.
            x=L+[0,g,g,0]';         % [L;R;R;L] edges of windows in building i, window column j
            z=ones(4,1)*(.2:.4:h(i)-.2)+[0;0;g;g]; % 4xn height of windows for n levels of windows; in a:b:c a is height of first row of windows, b is the interval between window-bottoms, c is height of tallest window.
            y=z*0+m(i)+.45;          % 4xn location of front face of building
            % Plot windows for building i, column j
            for w = 1:width(z)
                handles.w(end+1)=patch(z(:,w)*0+x,y(:,w),z(:,w),'y');
                if rand(1) < 0.25
                    handles.w(end).FaceColor = 'none';
                end
            end
        end
    end
    set(gcf,'color','k')
    axis off
    camva(2.5)
    campos([58.256 185.12 25.621])
    view([165,5])
    clim([0 8])
    xlim(xlim) % lazy way of freezing axis limits
    ylim(ylim)
    zlim([.8,8])
end
% randomly toggle on/off some window lights
p = 0.001;  % portion (0:1) of lights to toggle
n = numel(handles.w);
idx = randperm(n,ceil(n*p));
colors = {handles.w(idx).FaceColor};
isnone = cellfun(@ischar, colors);
set(handles.w(idx(isnone)), 'FaceColor', 'y')
set(handles.w(idx(~isnone)), 'FaceColor', 'none')
% Credit to 隆光 中村 for the fireworks
% https://au.mathworks.com/matlabcentral/communitycontests/contests/6/entries/13092
if f<10 % Trail
    t1 = linspace(1,7.5,15);
    U = sin(0.3*t1);
    h1 = 3;
    plot3(U(f:f+h1)+11,U(f:f+h1)*1.3,t1(f:f+h1),'w',LineWidth=0.7);
elseif f>=20 && f<30
    F = f-19;
    t1 = linspace(1,6,15);
    U = sin(0.3*t1);
    h1 = 3;
    plot3(U(F:F+h1)+7,U(F:F+h1)*1.3,t1(F:F+h1),'w',LineWidth=0.7);
end
if f>=10 % Explosion
    g = 9.8;
    m = 1;
    v0 = 50;
    k = 0.9;
    t2 = linspace(0,5,38);
    n = 9;
    p = 10;
    h = 10;
    circle_theta = linspace(0,2*pi,p);
    X = [11.9,7.9];
    Y = [6.2,5];
    c = 'mg';
    D = [.07,.05];
    for i = 1:2
        f = f-i*9;
        for j = 1:n
            theta = j*2*pi*(1/n);
            if f <= h
                ts = t2(1:f);
            elseif f >= h+1 && f <= 38-h
                ts = t2(f-h:f);
            elseif f >= 38-h+1 && f <= 39
                ts = t2(f-h:38-h);
            end
            x = (m*v0/k)*(1-exp(-k*ts/m))*cos(theta);
            y = (m/k)*((v0*sin(theta)+(m*g/k))*(1-exp(-k*ts/m))-g*ts);
            for q = 1:p
                d = D(i);
                plot3(d*x*cos(circle_theta(q))+X(i),d*x*sin(circle_theta(q))+1,d*y+Y(i),'w')
                plot3(d*(1/2)*x*cos(circle_theta(q))+X(i),d*(1/2)*x*sin(circle_theta(q))+1,d*(1/2)*y+Y(i),c(i))
            end
        end
    end
end
end


