MATHWORKS官方举办的迷你黑客大赛第三期(MATLAB Flipbook Mini Hack)圆满结束,虽然我的水平和很多大佬还有比较大的差距,但所有奖也算是拿满了:
专家评选前三名,以及投票榜前十:~
每周的阶段性获奖者:
下面正式开始!
本次比赛要求提交名为drawframe(f)
的函数代码,生成长达2s,48帧的动态图,动图生成是依靠如下代码来做到的:
function contestAnimator()
animFilename = 'animation.gif'; % Output file name
firstFrame = true;
framesPerSecond = 24;
delayTime = 1/framesPerSecond;
% Create the gif
for frame = 1:48
drawframe(frame)
fig = gcf();
fig.Units = 'pixels';
fig.Position(3:4) = [300,300];
im = getframe(fig);
[A,map] = rgb2ind(im.cdata,256);
if firstFrame
firstFrame = false;
imwrite(A,map,animFilename, LoopCount=Inf, DelayTime=delayTime);
else
imwrite(A,map,animFilename, WriteMode="append", DelayTime=delayTime);
end
end
end
闲话少叙,一起看作品!!
作品概览
Tim / Moonrun
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/16134
function drawframe(f)
if f == 1
%% Make landscape
clf
rng(4,'v4');
N=200;
q=linspace(-1,1,N);
k=abs(ifft2(exp(6i*randn(N))./(q.^2+q'.^2+1e-9)));
k=rescale(1./(k+1));
imagesc(k);
[x,y]=meshgrid(linspace(-1,1,N*7)*7);
k=repmat(k,[7,7]);
surf(x,y,k/2,'edgeC','none','SpecularS',0);
axis equal
colormap(copper)
hold on;
% Make moon
bscl = 1e5;
rng default
nl=1e4;
rd = rand(nl,1)/3+1;
po = randn(3,nl);
po = po./vecnorm(po);
[p,k,s]=SM(po',rd,7);
ms = mean(s);
ns = s - ms;
s = erf(ns*500)/50+ms;
[~,~,sb]=SM(po',randn(nl,1),1);
% Now, take a page from Adam D.' book for the craters
rng(1,'twister');
x = randn(3,16); % Let's add 16 craters
x = x./vecnorm(x);
mfc = @(x,y,z)y.*(erf((x-z)*30)/2+.5); % Using erf as the crater function...
for n = 1:size(x, 2)
d = vecnorm(x(:,n)-p');
s = mfc(d',s-1.14,rand(1)/2)+1.14;
end
s = s + sb/20;
s = (s-mean(s))/10+mean(s);
p2 = (p.'./vecnorm(p.'))'.*s;
E='EdgeC';
F='FaceC';
O='none';
G='FaceA';
% Plot moon
hold on;
bscl=1e1;
T2=trisurf(k,bscl*p2(:,1)+1*bscl,bscl*p2(:,2)+4*bscl,bscl*p2(:,3)+1*bscl,'FaceC','flat','FaceVertexCData',rescale(s,.4,1).*[1,1,1],E,O); % Plot
material([0,1,0,10]);
axis equal off
set(gcf,'color','k');
light('position',[1,-1,1]);
light('position',[1,-1,1]);
camproj p
campos([0,-7.2,.35]);
camva(40);
end
x = linspace(0, 1, 49)*2;
dx=mean(diff(x));
x = 0:dx:200*dx;
y = cos(pi*x)/15;
a=linspace(0,2*pi,49)-2;
% for n = 1:1:48
n=f;
cpos = [0.05, -7.2+2, .42] + [y(n), x(n), y(n)/2];
camup([0, sin(a(n))/2, cos(a(n))/2+1]);
campos(cpos);
camtarget([0.05, -7.2+2, .42] + [y(n+1), x(n+1), y(n+1)/2]);
1;
end
function [p,k,s]=SM(in,rd,r)
n=size(in,1);
k=convhull(in); % Points on "in" must lie on unit circle
c=@(x)sparse(k(:,x)*[1,1,1],k,1,n,n); % Connectivity
t=c(1)|c(2)|c(3); % Connectivity
f=spdiags(-sum(t,2)+1,0,t*1.)*r; % Weighting
s=((speye(n)+f'*f)\rd); % Solve for s w/regularizer
p=in.*s; % Apply
end
Tim / Planet and moon
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/16129
function drawframe(f)
% Procedural planet & moon
% Apologies for the sloppy code and a couple garbage variables. Based
% around Matlon5 code but forgot to add as a remix.
persistent V
if f==1
rng(5,'twister');
v=@vecnorm;
ra=@rand;
bscl = 1e5;
% Usng a random sphere instead of fibonacci sphere like Matlon5
nl=1e5;
r=ra(nl,1)/3+1;
p=randn(3,nl);
p=p./v(p);
[p,k,s]=SM(p',r,5e1);
% Spin points to make a gaseous looking surface...
p2 = (p.'./vecnorm(p.'))';
scl=-7;
p2(:,1:2) = [p2(:,1).*cos(p2(:,3)*scl)-p2(:,2).*sin(p2(:,3)*scl),p2(:,1).*sin(p2(:,3)*scl)+p2(:,2).*cos(p2(:,3)*scl)];
% Re-smooth
[p3,k2,s2]=SM(p2,s,3e0);
p3 = (p3.'./vecnorm(p3.'))';
% Add a storm in it
xp=[1,1,0.5]';
xp = xp/norm(xp);
p2=p3';
for nn = 1:100
pob = p2./v(p2);
for n = 1:size(xp, 2)
if n == 1
xc = XP(xp(:,n),pob);
else
xc = xc + XP(xp(:, n),pob);
end
end
p2 = (p2./v(p2) + xc/1500).*s';
end
p3 = (p2./vecnorm(p2))';
E='EdgeC';
F='FaceC';
O='none';
G='FaceA';
% Plot planet
T=trisurf(k2,p3(:,1)*bscl,p3(:,2)*bscl,p3(:,3)*bscl,s2,E,O); % Plot
material([0,1,0,10]);
% Make it a purple planet
c1=[88,72,154];
c2=[112,89,145];
c3=[140,120,140];
c4=[180,160,170];
c5=[250,240,242];
c=[c1;c2;c3;c4;c5];
cmp = interp1(1:5, c, linspace(1, 5, 256))/255;
colormap(min(cmp.^1.5*1.5,1));
cb = caxis;
% % % Add a moon...
rng default
v=@vecnorm;
nl=1e4;
rd = rand(nl,1)/3+1;
po = randn(3,nl);
po = po./vecnorm(po);
[p,k,s]=SM(po',rd,7);
ms = mean(s);
ns = s - ms;
s = erf(ns*500)/50+ms;
[~,~,sb]=SM(po',randn(nl,1),1);
% Now, take a page from Adam D.' book for the craters
rng(1,'twister');
x = randn(3,16); % Let's add 16 craters
x = x./vecnorm(x);
mfc = @(x,y,z)y.*(erf((x-z)*30)/2+.5); % Using erf as the crater function...
for n = 1:size(x, 2)
d = vecnorm(x(:,n)-p');
s = mfc(d',s-1.14,rand(1)/2)+1.14;
end
s = s + sb/20;
s = (s-mean(s))/10+mean(s);
p2 = (p.'./vecnorm(p.'))'.*s;
E='EdgeC';
F='FaceC';
O='none';
G='FaceA';
% Plot moon
hold on;
T2=trisurf(k,bscl*p2(:,1)/3+4*bscl,bscl*p2(:,2)/3-1*bscl,bscl*p2(:,3)/3,'FaceC','flat','FaceVertexCData',rescale(s,.4,1).*[1,1,1],E,O); % Plot
material([0,1,0,10]);
axis equal off
set(gcf,'color','k');
light;
light('color',[1,1,1]*.4);
caxis(cb);
V=hgtransform('Parent',gca);
set(T2,'parent',V);
camproj p;
camtarget([0,0,.2]*bscl);
end
a=-1.45:.018:-.59;
camva(9);
c=pi/4-sin(0:.131:2*pi)/10;
campos([sin(c(f)),cos(c(f)),0]*1.3e6);
set(V,'Matrix',makehgtform('zrotate',-a(f)));
end
function cp=XP(n,p)
n=n/vecnorm(n)*.9;
d=sqrt(sum((n - p).^2));
cp=cross(p, n.*ones(1,size(p,2)))./d.^2;
end
function [p,k,s]=SM(in,rd,r)
n=size(in,1);
k=convhull(in); % Points on "in" must lie on unit circle
c=@(x)sparse(k(:,x)*[1,1,1],k,1,n,n); % Connectivity
t=c(1)|c(2)|c(3); % Connectivity
f=spdiags(-sum(t,2)+1,0,t*1.)*r; % Weighting
s=((speye(n)+f'*f)\rd); % Solve for s w/regularizer
p=in.*s; % Apply
% S.D.G.
end
Adam Danz / A light in the attic
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/16114
function drawframe(f)
persistent h x y n wdw
rng default
if isempty(h) || f==1
% Dark sky
axes(Position=[0 0 1 1])
s = 99;
q=linspace(50,1,s);
hold on % hold before imagesc to keep ydir normal
imagesc(q'.*(q./q),[20,80])
% Add star rotation
td=0:0.001:.1; % theta delta
n=1000; % number of stars
theta = pi/1.8*rand(1,n)+pi-.1; % range: 180:270 +/- (quadrant III)
[x,y]=pol2cart(theta+td',rand(1,n).*s*sqrt(2));
x = x+s; % move polaris to the upper right corner
y = y+s; % move polaris to the upper right corner
h = plot(x, y,'color','w');
rgba = ones(n,4);
rgba(:,4) = rescale(randg(1,n,1),.1,1);
set(h,{'color'},mat2cell(rgba,ones(n,1),4))
% Add grassy hill
g = 1000; % number of grass blades
xg = linspace(0,s+1,g);
yg = cos(xg/(s+1))*30+randg(1,1,g);
area(xg,yg,FaceColor='k')
xg=[1,1:.1:s,s];
yg=[1,cos(0:.1/98:1)*30+randg(1,1,981),1];
fill(xg,yg,'k')
% Add house silhouette
fill([4 4 3 6 9 8 8]*5,[5 7 7 8 7 7 5]*5,'k')
% Add window
wdw=fill(30+2*[-1 1 1 -1],35+[2.2 2.2 .1 .1],[.1 .1 .1]);
camva(6)
axis equal off
xlim([1 s])
ylim([1 s])
colormap gray
end
k = round(f/48*height(x));
set(h,{'XData'},mat2cell(x(1:k,:),k,ones(1,n))',{'YData'},mat2cell(y(1:k,:),k,ones(1,n))')
if f==24
wdw.FaceColor = [0.6 0.6 0.4];
end
end
Eric Ludlam / Will O' Wisp
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15974
function drawframe(f)
E=4; % Size of the forest environment
% Abbreviations
J=@rand;
K=@rescale;
VN=@vecnorm;
persistent L
if f==1
set(gcf,'color','k');
% Select some nice tree locations
%v1=[0 0 1.3
% 1.4 0 1
% -1.3 0 1.2
% 1 -1 1.3
% -1 -1 1.2
% -.5 -2.1 .9
% .34 -2 1
% .7 -2.3 .8
% -1.5 -4 1
% .6 1 1.2
% .4 2 1.3
% -.55 1.5 1.4];
% Below is the compressed version of the above, saving almost 50 chars!
v1=reshape(K('啥猸㦴檳䀘䪿岣摏㕱戭巫䦮啥啥啥䀘䀘⢩⫋⑧0檳耀留焗檳滵焗滵梑檳晰檳滵焗猸'-'0',-4,2),12,3);
% Compressed version of color array: See parent of this remix
B=(['ÆJJ';'ûÁ';'F.';'¶eU';'_ ';'¡g<';'¢aa';'·9'-' '])/256;
G=([')';'B¬B';'pé';'ob';' ¡ ';'U~[';'JË«';'RîR']-' ')/256;
for i=1:size(v1,1)
%% Tree Trunks
N=30;
Q=.1; % variation in distance from center
RN=12; % n pts in bounding rings
rv=[.05 .02]; % Radius values
rh=[0 1]; % Radius heights
% Random pts on cylinder
rt=linspace(0,2*pi,RN+1);
rt(end)=[];
T=[J(1,N)*pi*2 rt rt];
h=[K(randn(1,N)) ones(1,RN)*rh(1) ones(1,RN)*rh(2)];
% Adjust the radius based on height
R=interp1(rh,rv,h);
pts=[cos(T).*R
sin(T).*R
h]';
% triangulate the perfect cylinder
tf=convhulln(pts);
% Push points in/out with variance of Q
D=(1-Q+J(1,size(pts,1))*(Q*2))';
tv=pts.*(D.*[1 1 0]+[0 0 1]);
mkP(tf,(tv+v1(i,:).*[1 1 0]).*[1 1 v1(i,3)+.1],i,B,D);
%% Tree tops
N=150;
% Alg for random distribution of pts on a sphere.
T=J(1,N)*pi*2;
u=J(1,N)*2-1;
pts=[0 cos(T).*sqrt(1-u.^2)
0 sin(T).*sqrt(1-u.^2)
0 u ]';
% triangulate the perfect sphere
lf=convhulln(pts);
% Push points around to make foliage frumphy
Q=.15;
D=(1-Q+J(1,size(pts,1))*(Q*2))';
lvr=pts.*D;
% Scale down into our world and push up into treetops
ss=v1(i,3)*.34;
llv=lvr.*[.12+ss .12+ss .08+ss]+[0 0 .1];
mkP(lf,llv+v1(i,:),i,G,D);
%% Bumpy high-res ground
N=400;
Q=.2;
% coordinates
T=J(1,N)*2;
R=J(1,N)+.05;
x=cospi(T).*R*E;
y=sinpi(T).*R*E;
% Triangulate the flat disc so we can draw it
pv=[x' y'];
pf=delaunay(pv);
% Variation
D=(J(1,size(pv,1))*Q)';
% flip faces due to normals needing to match trees
mkP(fliplr(pf),[pv D],4,G,D);
end
%% Our Wisp!
L=line(1,1,1,'Marker','.','Markers',20,'Color','y');
light('color','k'); % This light forces normals to be
% computed on the patches but since it
% is black, it has no visible effect.
%% Decorate!
set(gca,'position',[0 0 1 1],'vis','off','proj','p');
axis([-E E -E E -1 E]);
view(3);
daspect([1 1 1]);
campos([0 3 .5]);
camva(60);
camtarget([0 0 .7]);
drawnow; % Force all vertex normals to be computed
end
%% Update Light
lp=[cospi(f/24)*.5 sinpi(f/24)*1.5-.5 cospi(f/12)*.2+.7];
set(L,'XData',lp(1),'YData',lp(2),'ZData',lp(3));
%% Apply lighting to all the patches
O=findobj('type','patch');
for i=1:numel(O)
doL(O(i));
end
function doL(p)
% Perform a lighting operation on the object provided in p.
% This algorithm is an adaption from the ML lighting
% algorithm in webGL, but ignoring specular (to make it
% spooky) and adding in distance based ambient lighting,
% and adding range to the local light.
cp=campos;
v=p.Vertices;
% Distance from camera, extending to range.
gl_dist=K(min(VN(v-cp,2,2),max(E)),'InputMin',0,'InputMax',E*3);
% Weight of ambient lighting. Desaturate some of the red on tree in front
w_amb=[.3 .4 .4].*(1.1-gl_dist);
% Effects of the wisp
s_diff=.7; % diffuse strength
lr=1.5; % max distance light can illuminate
d=v-lp;
l_dist=min(VN(d,2,2),lr);
s_dist=1-K(l_dist,'InputMax',lr,'InputMin',0);
% Diffuse Weight
% Invert vertex normals due to faces being in wrong order and - being short
w_diff=L.Color.*dot(-p.VertexNormals,d./VN(d,2,2),2)*s_diff;
% CbaseColorFactor (ambiant and diffuse)
bcf = min(max(w_diff,0), 1).*s_dist;
% Accumulate all the terms and put on patch as color
set(p,'FaceVertexCData',min(p.UserData.*(w_amb+bcf), 1));
end
%% Shorten patch creation
function mkP(f,v,i,C,D)
% f - faces
% v - vertices
% i - thing index
% C - Array of colors to pick from
% D - distance array
% Create our colors based on D
bC=C(mod(i,size(C,1))+1,:);
C2=hsv2rgb(rgb2hsv(bC).*[.1 1 .3]);
q=bC-C2;
fvc=K(D)*q+C2;
% Create patch and stash colors
patch('Faces',f,'vertices',v,'EdgeC','n','FaceC','i',...
'Amb',1,'FaceL','g','FaceVertexC',fvc,'U',fvc);
end
end
ME / View from a train window
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15834
function drawframe(f)
persistent Y
if(f==1)
r = rand(1,4000);
for i=1:5
Y(i,1:800) = smoothdata(smoothdata(r(800*(i-1)+1:i*800)));
end
end
clf
hold on
phi = linspace(0,1,4);
patch([1 30 30 1],[0 0 1.5 1.5],phi);
colormap(sky);
c = bone(8);
m = fliplr(1:5);
for i=5:-1:1
area(Y(i,1+(m(i)-1)*f:30+(m(i)-1)*f)+(i/10),'FaceColor',c(i,:),'EdgeAlpha',0)
end
hold off
axis([1 30 0 1.5])
set(gca,'XTick',[],'YTick',[])
end
Tim / Snowfall
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15824
function drawframe(f)
clf
% Face parameters for the mother (right) and child (left) that control
% things such as cheek shape, general head structure, eyes (open v. closed)
HP=[2,4;.35,.45;8,4;.35,.3;.48,.52;1.28,1.32;2.3,2.2;1,-1;-.4,.3;.8,.75;4,20];
% Mother's face
[x,y,z]=face(400,HP(:,2));
% Rotation
a=@(x,y,r)x*cos(r)-y*sin(r);
b=@(x,y,r)x*sin(r)+y*cos(r);
% Plot
r=.17;
S=@(x,y,z)surf(x,y,z,'EdgeC','none','FaceC',[1,1,1],'DiffuseS',.2,'SpecularE',1,'SpecularS',1,'AmbientS', 0.0);
S(a(x,y,r)-.2,z,b(x,y,r)-.1);
axis equal off
set(gcf,'color','k');
% Light
light('pos',[-1.3,-1,.6]);
camzoom(2.8);
hold on;
% Child's face
[xa,ya,z2] = face(400,HP(:,1));
x2=a(xa,ya,.1);
r=.35;
S(-b(x2,z2,r)*.8-.8, a(x2,z2,r)*.8+.4, b(xa,ya,.1)*.8-.8);
camva(5)
campos([-7,-15,3]);
camtarget([-.6,0,-.45]);
rng default
N=99;
r=@rand;
a=r(N,1)*2*pi;
z=r(N,1);
a=a+(0:.25:2)*pi;
z=z.*ones(1,9);
d=r(N,1)*2+1;
d=d.*ones(1,9);
g=linspace(0,pi/4,49);
a=[a;a;a;a;a];
d=[d;d;d;d;d];
d=d(:);
z=[z;z-1;z+1;z-2;z+2];
c=a(:)+g(f);
scatter3(d.*cos(c)*2-.6,d.*sin(c)*2,z(:)-f/49,rescale(-d.*(cos(c)+sin(c)))*99+1,'w','filled','MarkerFaceA',.5);
plot3(0,1,-2,'k.','markers',1000)
end
function [x,y,z]=face(n,P)
% Start with a basic sphere
[x,y,z]=sphere(n);
% General distortions to make it more face like
z=erf(z*P(10));
x(y<0)= x(y<0)-abs(y(y<0)).^P(1)*P(2);
z(y<0)= z(y<0).*(1-.5*y(y<0).^P(3));
z(y>0)=z(y>0).*(1-.1*y(y>0).^2);
x(x<0)= erf(0.6*x(x<0))/0.6;
x(x>0)=x(x>0)*.9;
y(y>0)=y(y>0)*.9;
y=y+P(4);
z(y>0)=z(y>0).*exp(-x(y>0).^2/3).^y(y>0);
y=y-.3;
x=x+.6;
x(x>0)=x(x>0)*.9;
x=x-.6;
m=@(x,o,s).5-erf(-s*x+o).*erf(s*x+o)/2;
c=y(x<0);
c=1-exp(-(c+(exp(c*3)-0.4)).^2*3).*(.7*m(z(x<0),.8,10)+.3).*.1;
x(x<0)=c.*x(x<0);
fc=@(x)exp(-(-x+(exp(-x*9)-.6)).^2*2);
fc2=@(x)exp(-(-x+(exp(-x*2)-.6)).^2*2);
% Adding nose & lips
x(x<0)=((1-1*m(z(x<0),2,15)).*(fc(y(x<0)*1+P(5))*.15)+1).*x(x<0);
x(x<0)=((1-1*m(z(x<0),.7,8)).*(fc(y(x<0)*2+P(6))*.08)+1).*x(x<0);
x(x<0)=((.7-.7*m(z(x<0),0,8.5)).*(-fc(-y(x<0)*P(7)-1.71)*.06)+1).*x(x<0);
x=x+.5;
x(x>0)=x(x>0).*(1-.35*fc2((y(x>0)+.95)/1.4));
x=x-.5;
z=z.*(((x-.2).^2+(y+.4).^2).^.4*.2+.8);
x=x+.5;
% Adding eyes
[x,y,z]=ey(x,y,z,P,1);
[x,y,z]=ey(x,y,z,P,-1);
end
function [x,y,z]=ey(x,y,z,P,s)
b=@(x)tanh(exp(-4*abs(x).^2.5));
xt=(z(x<0)+s*0.33)*5;
yt=(y(x<0)+.145)*7;
ofs=max(b2(yt+P(8)*.2*xt.^2,P(9),P(11)).*b(sqrt(xt.^2+yt.^2)), b(sqrt(1.5*xt.^2+yt.^2))*.7);
x(x<0)=x(x<0)-ofs/20;
end
% Bump function with edge for eyes
function m=b2(x,of,s)
m=(erf((x+of)*s)/1.5+.5)+exp(-(x+(of-.1)).^2*10)/4;
%S.D.G.
end
Nikolay Velkov / O Christmas Tree
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15939
function drawframe(f)
% This is Xor's AMAZING shader
% https://www.shadertoy.com/view/7lKSzw
% Here is my translation to MATLAB
iRes = 300;
iTime = f/48*2;
persistent fragCoord
if isempty(fragCoord)
[x,y]=meshgrid(1:iRes, 1:iRes);
fragCoord = cat(3,x,flip(y,1));
end
im = mainImage(fragCoord);
im = imresize(im, [3*iRes, 3*iRes], 'method', 'bilinear');
imshow(im);
axis equal
axis off
%% magic
function finalColor = mainImage(fragCoord)
finalColor = zeros(iRes, iRes, 3);
r = cat(3,iRes,iRes);
for i=0:400
if mod(i,8) < 6
even = mod(i,2);
c = [even,1- even,0];
else
c = [1 1 1];
end
p = (fragCoord.* 2 - r)./r(:,:,2).*300 + cat(3,0,i-230)....
+ cat(3,i+mod(i,99),i/4).*cos(iTime.*0.5+cat(3,i,i+11)).*0.5;
for j = 1:3
finalColor(:,:,j) = finalColor(:,:,j) + c(j)./vecnorm(p,2,3).^2;
end
end
finalColor = sqrt(finalColor./0.1);
end
end
HyunGwang Cho / Nyancat
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15839
function drawframe(f)
n=nan;
% resample
f = mod(ceil((f)/2)-1,12)+1;
% canvas
C=ones(69,124)*13;
% rainbow
r=([1;1;1]*(7:12));
r=r(:);
r1=28:45;r2=r1-1;
if mod(f,4)<2
r1=r2;r2=r1+1;
end
C(r1,[1:6,15:22,31:38,47:53])=repmat(r,[1,29]);
C(r2,[7:14,23:30,39:46])=repmat(r,[1,24]);
% stars
a=[2,4,2,0,0,3];
a=mod(a+f-1,6);
y=[-1,8,20,42,54,64];
x=[61,3,21,30,9,55];
x=x-(f-1)*7;
for i=1:6
C=p(C,x(i),y(i),s(a(i)));
C=p(C,x(i)+84,y(i),s(a(i)));
end
% tail (detail)
% z=nan(10,7);
% a=z;a(3:5,3:4)=1;a(5:6,5:7)=1;a([4,7],6:7)=0;a(3:4,[2,5])=0;
% a([22,25,32,36,46])=0;t{1}=a;t{5}=a;t{7}=a;t{11}=a;
% a(2,:)=n;a([4,5,15,26,56,65])=0;a([14,25])=1;t{4}=a;t{10}=a;
% a=z;a([12:11:67,13:11:46,31:11:64,41:11:63,11,21,66])=0;
% a([22:11:55,32:11:65])=1;t{6}=a;t{12}=a;
% a=z;a(7:8,3:6)=1;a(67)=1;a([6,9],4:6)=0;
% a([17,18,27,29,58,65,66,68])=0;t{2}=a;t{8}=a;
% a([17,59,56,66])=n;a([19,30,40,55])=0;a([29,39])=1;t{3}=a;t{9}=a;
% C=p(C,46,33,t{f});
% tail (simple)
z=nan(10,7);
a=z;a(3:5,3:4)=1;a(5:6,5:7)=1;a([4,7],6:7)=0;a(3:4,[2,5])=0;
a([22,25,32,36,46])=0;t{1}=a;t{6}=a;t{7}=a;t{12}=a;
a(2,:)=n;a([4,5,15,26,56,65])=0;a([14,25])=1;t{2}=a;t{5}=a;t{8}=a;t{11}=a;
a=z;a(7:8,3:6)=1;a(67)=1;a([6,9],4:6)=0;
a([17,18,27,29,58,65,66,68])=0;t{3}=a;t{4}=a;t{9}=a;t{10}=a;
C=p(C,46,33,t{f});
% foot
a=nan(3,6);a(:,2:5)=0;a(1:2,3:4)=1;a(6)=n;a(16)=0;
x=[6,7,6,4,4,6];x=[x,x]+50;x=x(f);
y=[3,4,4,4,4,3];y=[y,y]+40;y=y(f);
C=p(C,x,y,a);C=p(C,x+10,y,a);C=p(C,x+15,y,a);
a=zeros(5,4);a([1,2,6,20])=n;a(3:4,2:3)=1;a(18)=1;
C=p(C,x-4,y-2,a);
% body
a=zeros(18,21);a([1,2,18,19])=n;a(2:17,2:20)=4;a([20,35])=0;
a(3:16,3:19)=5;a([39,40,51,52,57,70])=4;a(:,21:-1:18)=a(:,1:4);
b=[66,77,87,100,140,152];a([b,b+143])=6;
y=ones(1,12);y([1,6,7,12])=0;y=y+26;
C=p(C,53,y(f),a);
% head
a=ones(13,16);a([1:5,11:14,25:26,39])=n;
a(1,5:9)=n;a(2,6:9)=n;a(3,7:9)=n;
a(6:9,1)=0;a(2:5,2)=0;a([10:14:52,40:14:82,27])=0;
a(13,5:9)=0;a(4,8:9)=0;a(:,16:-1:8)=a(:,1:9);
a(9:10,[3,4,14,15])=2;a(11,6:12)=0;a([75,114,153,125])=0;
a(7:8,[5,6,12,13])=0;a([59,150])=3;
x=[1,1,1,0,0,0];x=[x,x]+63;
y=[0,1,1,1,0,0];y=[y,y]+31;
C=p(C,x(f),y(f),a);
% color map
m=[0,0,0;164,164,164;248,176,165;254,254,254;
252,218,170;247,185,251;245,123,188;245,62,31;
250,167,52;254,238,71;89,216,58;19,154,248;
109,92,247;0,78,147]/255;
% draw
imshow(uint8(C),m);
end
function b=s(n)
x4=@(a) a([1:4,3:-1:1],[1:4,3:-1:1]);
z=nan(4);
a=z;a(16)=3;s{1}=x4(a);
a=z;a([12,15])=3;s{2}=x4(a);
a=z;a([8,12,14,15])=3;s{3}=x4(a);
a=z;a([4,8,13,14,16])=3;s{4}=x4(a);
a=z;a([4,6,13])=3;s{5}=x4(a);
a=z;a([4,13])=3;s{6}=x4(a);
b=s{n+1};
end
function Y=p(I,x,y,S)
Y=padarray(I,[84,84]);
a=y+83+(1:size(S,1));
b=x+83+(1:size(S,2));
Y_=Y(a,b);
i=~isnan(S);
Y_(i)=S(i);
Y(a,b)=Y_;
Y=Y(85:153,85:208);
end
Daniel Pereira / Earth, our only home
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15769
function drawframe(f)
persistent x;
if f==1
gx = geoaxes(gcf,'Position',[0 0 1 1],'Basemap','colorterrain','grid','off','tickdir','none');
gx.Scalebar.Visible = "off";
geolimits([-90 90],[-180 180]);
x = getframe(gcf);
x = x.cdata;
end
set(gcf,'color','k');
rng(41);
scatter3(2*rand(20,1)-1,2*rand(20,1)-1,-ones(20,1),'.w'); hold on;
scatter3(2*rand(20,1)-1,ones(20,1),2*rand(20,1)-1,'.w');
scatter3(ones(20,1),2*rand(20,1)-1,2*rand(20,1)-1,'.w');
lt = linspace(-90,90,size(x,1));
ln = linspace(-180,180,size(x,2));
[LT,LN] = meshgrid(lt,ln+7.5*f);
surf(cosd(LT').*cosd(LN'),cosd(LT').*sind(LN'),-sind(LT'),'cdata',(double(x)/255).^2,'edgecolor','none');
axis equal off;
hold off;
% camlight(30,25);
light('style','infinite','color',[0.95 1 0.9],'position',[cosd(180)*cosd(45) cosd(180)*sind(45) sind(180)]);
material dull;
end
Nikolay Velkov / Collatz Tree
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15617
function drawframe(f)
%% Draw the Collatz tree
% initialize cycle
G = graph([1 2],[2 4]);
% starting number
n = 4;
% depth
depth = round(f^(3/4));
% do recursion
G = collatz(G,n,depth);
% remove auto-added isolated nodes
isolated_nodes = find(degree(G) == 0);
G = rmnode(G,isolated_nodes);
% create figure
fig = figure;
colormap cool
p = plot(G);
layout(p,'layered','direction','right')
for j = 1:G.numedges
edge = table2array(G.Edges(j,:));
labelnode(p,edge,string(edge));
end
p.NodeCData = cellfun(@str2double, p.NodeLabel);
p.MarkerSize = 4;
p.LineWidth = 1;
p.NodeLabel = {};
p.EdgeColor = 'w';
cbar = colorbar;
cbar.Color = 'w';
cbar.FontSize = 12;
axis off
fig.Color = 'k';
end
%% Collatz conjecture rule
function G = collatz(G,n,depth)
if mod(n,3) == 2
R = [2*n, (2*n-1)/3];
else
R = 2*n;
end
depth = depth -1;
if ~depth
return
end
for j = 1:length(R)
G = G.addedge(n,R(j));
G = collatz(G,R(j),depth);
end
end
O / Skywheel
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15367
function drawframe(f)
persistent u w c gu g1 g2 c1 cd a0;
cp=@(a,c) colormap(a,c);
if f==1
rd=@(v) round(v);
sf=@(x,y,z) surface(x,y,z,EdgeColor='no');
n=1e2;
[x,y,z]=mkp(10,9.8,n);
z=.2*z-.1;
[x1,y1,z1]=sphere(4);
x1=.1*rd(x1);
y1=.1*rd(y1);
z1=12*rd(z1);
[z2,y2,x2]=mkp(1,.95,n);
z2=z2+12;
x2=.1*x2-.05;
[z3,y3,x3]=mkp(.9,.01,n);
z3=z3+12;
x3=.9*x3-.45;
a0=axes;
view(3);
r0=30;
[x0,y0,z0]=sphere(n);
sf(r0*x0,r0*y0,r0*z0);
hold on;
c0=cp(a0,'abyss');
c0=flip(c0);
c0(:,1:2)=c0(:,1:2)/2;
c0(:,3)=c0(:,3)/1.4;
c1=cp(a0,'sky');
cp(a0,c1);
cd=(c0-c1)/48;
rs=25;
ns=2e2;
ts=rand(ns, 1)*pi+pi;
ps=acos(rand(ns, 1));
xs=rs*sin(ps) .* cos(ts);
ys=rs*sin(ps) .* sin(ts);
zs=rs*cos(ps);
rdsz=3*rand(ns, 1);
rdc=repmat((rand(ns, 1)+1)*.5, 1, 3);
u=scatter3(xs,ys,zs,rdsz,rdc,'fi');
gu=hgtransform(Pa=a0);
set(u,Pa=gu);
campos([-10 -2 5]);
camtarget([0 -15 10]);
camva(49);
axis equal off;
a1=axes;
view(3);
cm=cp(a1,'spring');
cm=cm/2;
cp(a1,cm);
material metal;
light(Po=[-10 0 20],St='local');
light(Po=[-10 -30 5],St='local');
light(Po=[-20 -20 20],Col=[.7 .7 .5],St='local');
axis equal off;
s=[1 .8 .7];
for i=1:3
x=s(i)*x;
y=s(i)*y;
z=s(i)*z;
w(i+12)=sf(z-.5,y,x);
w(i+14)=sf(z+.5,y,x);
end
t=pi/6;
rx=[1 0 0;0 cos(t) -sin(t);0 sin(t) cos(t)];
for i=1:6
[x1,y1,z1]=rot(x1,y1,z1,rx);
w(i)=sf(x1-.5,y1,z1);
w(i+6)=sf(x1+.5,y1,z1);
end
for i=1:12
[x2,y2,z2]=rot(x2,y2,z2,rx);
c(i,1)=sf(x2-.4,y2,z2);
c(i,2)=sf(x2+.4,y2,z2);
end
for i=1:12
[x3,y3,z3]=rot(x3,y3,z3,rx);
c(i,3)=sf(x3,y3,z3);
end
alpha(c(:,3),0.2);
g1=hgtransform(Pa=a1);
set(w,Pa=g1);
g2=hgtransform(Pa=g1);
set(c,Pa=g2);
campos([-10 -2 5]);
camtarget([0 -15 10]);
camva(49);
end
r=f*pi/6/48;
rx1=makehgtform(xrotate=r);
tl1=makehgtform(translate=[0 -sin(r) -cos(r)]);
set(g1,Ma=rx1);
set(g2,Ma=tl1);
rx2=makehgtform(yrotate=pi/9,zrotate=r/10);
set(gu,Mat=rx2);
if f<=24
c1=c1+cd;
alpha(u,f/24)
else
c1=c1-cd;
alpha(u,(48-f)/24)
end
cp(a0,c1);
end
function [x,y,z]=mkp(or,ir,n)
x=zeros(5,n+1);
y=x;
z=x;
[x([1 4],:),y([1 4],:),z([1 4],:)]=cylinder(ir,n);
[x(2:3,:),y(2:3,:),z(2:3,:)]=cylinder(or,n);
x(5,:)=x(1,:);
y(5,:)=y(1,:);
z(5,:)=z(1,:);
end
function [x,y,z]=rot(x,y,z,t)
r=@(v,s) reshape(v,s);
s=size(x);
p=[x(:) y(:) z(:)]';
p=(t*p)';
x=r(p(:,1),s);
y=r(p(:,2),s);
z=r(p(:,3),s);
end
Tim / Icy Comet
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15262
function drawframe(f)
% Icy comet
persistent frms
if f == 1
% A problem with texturing a sphere is what to do about the poles. It is difficult
% to texture the [x, y, z] surface matrices created using the
% sphere command without getting significant anisotropy, pinching and/or
% discontinuities near the top and bottom.
%
% In this example a randomly sampled sphere will be generated, then uniform noise will be added,
% followed by smoothing using the the connectivity matrix relating adjacent points. The
% smoothing allows the noise distribution to be made more realistic and
% avoids the pole pinching artifacts & discontinuities.
% % % Creating the comet nodes
% Random sphere used here: replace with a fibonacci sphere for a different
% effect
% Predictable
rng(7,'twister');
% Number of vertices on Asteroid
n=5e4;
% Randomly sampled sphere
g=randn(3,n);
p=g./vecnorm(g);
% % % Determining connectivity between points
k=convhull(p');
c=@(x)sparse(k(:,x)*[1,1,1],k,1,n,n);
t=c(1)|c(2)|c(3);
% % % Create the roughness penalizer
w=spdiags(-sum(t,2)+1,0,t*1.); % t*1. is to convert t from logical to double
% % % Random radial distance function to be penalized:
r=rand(n,1);
% % % Solve for smoothed surface. Smoothing will be different for ice vs.
% rock components.
s=(speye(n)+1e3*w'*w)\r-.43;
s2=(speye(n)+3e3*w'*w)\r-.4; % Smoother for the ice component
s=s/mean(s);
s2=s2/mean(s2);
% % % Apply surface to vertices for rock & ice components
p1 = p.*s';
p2 = p.*s2';
% % % Plot.
T(1)=trisurf(k,p2(1,:),p2(2,:),p2(3,:),'FaceC', 'w', 'EdgeC', 'none','AmbientS',0,'DiffuseS',1,'SpecularS',0);
hold on;
T(2)=trisurf(k,p1(1,:),p1(2,:),p1(3,:), s,'EdgeC', 'none','AmbientS',0);
colormap(flipud(gray));
caxis([-1,1]*std(s)+1);
T(1).Parent.Parent.Position(3:4) = [1120, 840];
% % % For looping
H=hgtransform('Parent',gca);
H2=hgtransform('Parent',gca);
set(T,'parent',H);
% Add stars in the background
rd = rand(n,1);
C=scatter3(p(1,:)*50, p(2,:)*50, p(3,:)*50, rd*300, [1,1,1].*rd,'.');
set(C,'parent',H2);
% % % pretty
axis equal off % Pretty
set(gcf,'color','k');
camproj p
camva(6);
camtarget([0,0,0.3]);
light('position',[2,-3,10],'style','local');
light('position',[2,-3,10],'style','local','color',[1,1,1]*.3);
ags = linspace(0, pi/3, 48);
for n = 1:48
set(H, 'Matrix', makehgtform('xrotate',n/(16*pi)));
set(H2, 'Matrix', makehgtform('zrotate',n/(16*pi)));
camtarget([0,0,0.3]);
campos([21*sin(ags(n)), -21*cos(ags(n)), 0]);
frms{n}=getframe(gcf);
end
cnt = 1;
close
end
image(frms{f}.cdata);
axis equal off
camva(4.5);
% S.D.G.
end
Nikolay Velkov / Energy
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/16004
function drawframe(f)
% This is Xor's AMAZING shader
% https://www.shadertoy.com/view/cltfRf
% Here is my translation to MATLAB
iRes = [800 450];
iTime = f/16;
persistent fragCoord
if isempty(fragCoord)
[x,y]=meshgrid(1:iRes(1),iRes(2):-1:1);
fragCoord = cat(3,x',y');
end
im4 = mainImage(fragCoord);
imr = imresize(im4, flip(iRes).*2, 'method', 'bilinear');
im = imshow(imr);
function finalColor=mainImage( fragCoord )
finalColor = zeros(iRes(1), iRes(2), 3);
r = cat(3,iRes(1),iRes(2));
for i = 0:0.01:1
% Center and scale outward
p = (fragCoord.* 2 - r)./iRes(2).*i;
% Compute z
z = max(1 - vecnorm(p,2,3).^2, 0);
% Sphere distortion
p = p./(0.2 + sqrt(z).*0.3);
% Offset for hex pattern
p(:,:,1) = p(:,:,1)./0.9 + iTime;
p(:,:,2) = p(:,:,2) + mod(ceil(p(:,:,1))*0.5, 1) + iTime * 0.1625;
% Mirror quadrants
v = abs((mod(p, 1))-0.5);
% Add color
c = [2, 3, 5]./2e3;
d1 =max(v(:,:,1).*1.5+ v(:,:,1), v(:,:,1).*2);
d2 =max(v(:,:,1).*1.5+ v(:,:,2), v(:,:,2).*2);
d3 =cat(3,d1,d2);
for j = 1:3
finalColor(:,:,j) = finalColor(:,:,j) + c(j).*z./(abs(d3(:,:,2) - 1) + 0.1 - i * 0.09);
end
end
finalColor = tanh(finalColor.^2);
finalColor = permute(finalColor,[2 1 3]);
end
end
Lateef Adewale Kareem / Six-Linked Bars Mechanism
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/15492
function drawframe(f)
persistent L1 L2 L3 L4 L5 L6 L7 L8 t2 t3 t4 t5 t6 ...
Bar2 Bar3 Bar4 Bar5 Bar6 f1 f2
if f==1
L1=5;
L2=2;
L3=6;
L4=4;
L5=6;
L6=4;
L7=5;
L8=1;
f1=@(t2,t)(L2*cos(t2)-(L1+L4*cos(t)))^2+(L2*sin(t2)-L4*sin(t))^2-L3^2;
f2=@(x5,y5,T)[x5+L5*cos(T(1))-(L1+L7+0.9*L6*cos(T(2)))
y5+L5*sin(T(1))-(L8+0.9*L6*sin(T(2)))];
t=linspace(0,2*pi,7);
brown=[206,179,140]/255;
s=sin(t);
c=cos(t);
R=1.15;
wclr=0.7*[1,1,1];
hold on;
fill(R*c,R*s,wclr);
fill(L1+R*c,R*s,wclr);
fill((L1+L7)+R*c,L8+R*s,wclr);
xline(0);
yline(0);
axis equal
t4=acos(((L2+L3)^2-L1^2-L4^2)/(2*L1*L4));
c4=cos(t4);
s4=sin(t4);
t2=asin(sin(t4*L4/(L2+L3)));
c2=cos(t2);
s2=sin(t2);
x3=L2*c2;
y3=L2*s2;
t3=atan2(L4*s4-L2*s2,L1+L4*c4-L2*c2);
x5=L2*c2+(2/3)*((L1+L4*c4)-L2*c2);
y5=L2*s2+(2/3)*(L4*s4-L2*s2);
t56=fsolve(@(T)f2(x5,y5,T),[1.5*pi;1.5*pi]);
t5=t56(1);
t6=t56(2);
Bar2=BarMaker(L2,'g',0);
Bar2.Rotate(t2);
Bar3=BarMaker(L3,'b',[0,0.45]);
Bar3.Rotate(t3);
Bar3.Trans([x3,y3]);
Bar4=BarMaker(L4,'g',0);
Bar4.Rotate(t4);
Bar4.Trans([L1,0]);
Bar5=BarMaker(L5,brown,0.3);
Bar5.Rotate(t5);
Bar5.Trans([x5,y5]);
Bar6=BarMaker(L6,'r',0);
Bar6.Rotate(t6);
Bar6.Trans([L1+L7,L8]);
axis([-2.6,11.4,-5,5])
else
t2=t2+2*pi/48;
c2=cos(t2);
s2=sin(t2);
x3=L2*c2;
y3=L2*s2;
t4=fzero(@(t)f1(t2,t),t4);
c4=cos(t4);
s4=sin(t4);
t3=atan2(L4*s4-L2*s2,L1+L4*c4-L2*c2);
x5=L2*c2+(2/3)*((L1+L4*c4)-L2*c2);
y5=L2*s2+(2/3)*(L4*s4-L2*s2);
t56=fsolve(@(T)f2(x5,y5,T),[t5;t6]);
t5=t56(1);
t6=t56(2);
Bar2.Rotate(t2);
Bar3.Rotate(t3);
Bar3.Trans([x3,y3]);
Bar4.Rotate(t4);
Bar5.Rotate(t5);
Bar5.Trans([x5,y5]);
Bar6.Rotate(t6);
end
end
function B=BarMaker(L,clr,F)
t1=pi/2+linspace(0,pi);
t2=t1+pi;
s1=sin(t1);
s2=sin(t2);
c1=cos(t1);
c2=cos(t2);
Xl=[0.5*c1,L+0.5*c2];
Yl=[0.5*s1,0.5*s2];
X=Xl;
Y=Yl;
Xl=[Xl,Xl(1)];
Yl=[Yl,Yl(1)];
for n=1:numel(F)
f=F(n);
X=[X,f*L,f*L+0.3*[c1,c2],f*L];
Y=[Y,0.5,0.3*[s1,s2],0.5];
Xl=[Xl,nan,f*L+0.3*[c1,c2]];
Yl=[Yl,nan,0.3*[s1,s2]];
end
ax=gca;
H=hgtransform('Parent',ax);
K=hgtransform('Parent',H);
B.line=plot(Xl,Yl,'k','Parent',K);
B.fill=fill(X,Y,clr,'EdgeColor','none','Parent',K);
B.Rotate=@(t)set(K,'Matrix',makehgtform('zrotate',t));
B.Trans=@(p)set(H,'Matrix',makehgtform('translate',[p,0]));
end
完
本次活动圆满结束,想参加只能等明年了,可以在比赛页面蹲一下:
https://ww2.mathworks.cn/matlabcentral/contests.html