- /
-
The MATropolis Five-O
on 3 Dec 2023
- 9
- 182
- 0
- 4
- 1997
drawframe(1);
This is a remix of Shanshan's 2D car.
The hit-and-run suspect was arrested by the MATropolis Five-O and is once again a crime-free city.
Write your drawframe function below
function drawframe(f)
persistent hg v hg2 hg2L hg2g
if f==1 || isempty(hg)
%% plot the city
rng default
set(gcf,color='k')
n=20;
bh=randg(1,n); % building heights
bh(9,2:end)=nan; % make way for a street
v=[16,8];
bh(:,v)=nan; % remove 2 horizontal rows to create streets
colormap gray
camproj per
hold on
axis equal off
b=bar3(bh);
set(b,{'CData'},get(b,'ZData'),{'FaceC'},{'i'})
clim([-1 5])
camva(30)
campos([21.3 8.8 1])
camtarget([8.6 9.6 .6]);
% Add windows
for i=1:n % rows of buildings
for j=1:n % columns of buildings
c=0.1:0.2:bh(i,j)-.2;
d=[c;c];
z=d(:)'+[0;0;.1;.1];
y=i+[-.2,.2]+[-1;1;1;-1]/12;
y=repmat(y,size(c));
ison=logical(rand(1,numel(d))>0.33); % some window lights will be off
rgb=[j,j]./n.*0.5+0.5; % R & G range 0.5 to 1, back to front
rgb(3)=1-rgb(1); % B ranges 0.5 to 0, back to front
patch(z(:,ison)*0+j+.45,y(:,ison),z(:,ison),rgb);
patch(z(:,~ison)*0+j+.45,y(:,~ison),z(:,~ison),[.19 .18 .03]);
end
end
% Add ground
q=2e3;
Ig=imgaussfilt(rand(q),2);
bx=linspace(0,n+1,q);
Igclim=[-1,1];
Igs=(Ig-min(Ig(:)))/range(Ig(:))*range(Igclim)+Igclim(1); % Scaled to clim
surf(bx,bx,zeros(q)+.05,FaceC='tex',CData=Igs,EdgeColor='n');
% Add Shanshan's 2D car, for now it will be oriented on the XY plane
hg=hgtransform(Par=gca);
patch(Faces=1:7,Vertices=[0.4 -3.2;0.4 0.0;1.4 -0.4;1.4 -0.8;2.4 -1.2;2.4 -3.2;0.4 -3.2],FaceC='k',Par=hg) % body
patch(Faces=1:4,Vertices=[1.6 -1.7;1.6 -1.2;2.1 -1.4;2.1 -1.7],FaceC='y',Par=hg) % window
rectangle(Position=[0 -1.5 1 1],Curv=1,FaceC='y',Par=hg) % front wheel
rectangle(Position=[0 -3 1 1],Curv=1,FaceC='y',Par=hg) % back wheel
rectangle(Position=[.25 -1.25 0.5 0.5],Curv=1,FaceC='k',Par=hg) % front inner wheel
rectangle(Position=[.25 -2.75 0.5 0.5],Curv=1,FaceC='k',Par=hg) % back inner wheel
% Create MATropolis public safety patrol car
hg2=copyobj(hg,gca);
hg2.Children(end).FaceColor='#3F57A9';
hg2L=rectangle(Position=[2.4 -2 0.4 0.3],Curv=.3,FaceC='b',Par=hg2); % roof light
% Add light glow
[th,rh]=meshgrid(linspace(0,2*pi,20),linspace(0,2,20));
[gx,gy]=pol2cart(th,rh);
hp = hg2L.Position;
hg2g=surf(gx+hp(1),gy+hp(2),0*gx,FaceC='r',EdgeC='n',FaceA='i',AlphaData=-rh,Par=hg2);
alim([-2,1])
end
% light sequence
r=[1 .5 .5];
b=[.5 .5 1];
w=[1 1 1];
seq=repmat([r;r;w;r;r;w;b;b;w;b;b;w],4,1); % light sequence
set([hg2L,hg2g],FaceC=seq(f,:))
% you are looking at the YZ plane
step=f/48*max(ylim)/3;
if f<27
hg.Matrix=makehgtform(translate=[v(1),7.7+step,0],yrotate=-pi/2,Scale=0.2); % rotate from the XY plane to the YZ plane and move to empty street
hg2.Matrix=makehgtform(translate=[v(1),6.3+step,0],yrotate=-pi/2,Scale=0.2);
else
hg2.Matrix=makehgtform(translate=[v(2),14.5-step,0],Scale=0.2,zrotate=pi,yrotate=-pi/2);
hg.Matrix=makehgtform(translate=[v(2),14.5-step-1,0],Scale=0.2,zrotate=pi,yrotate=-pi/2);
end
end