- /
-
Is this cheating?
on 31 Oct 2024
- 58
- 297
- 0
- 4
- 1990
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent H cP nx px ny py nz pz cPt c m pP pM A T CF AF U ID O
% setup
if f==1
%% helper functions
CF = @cellfun;
AF = @arrayfun;
U = 'UniformOutput';
F = @find;
O = @permute;
R = @repmat;
ID = eye(3);
pP = @(a,b) CF(@(pt) pt+a,b,U,0);
pM = @(a,b) CF(@(pt) pt-a,b,U,0);
%% figure/axes
% store axes handle
H = gca();
% set axes props
set(H,...
'Visible','off',...
'Units','Normalized',...
'InnerPosition',[0 0 1 1],...
'XLim',[-2.75 2.75],...
'YLim',[-2.75 2.75],...
'ZLim',[-2.75 2.75],...
'NextPlot','Add',...
'DataAspectRatio',[1 1 1],...
'PlotBoxAspectRatio',[1 1 1]);
% set figure backgroud color
set(gcf,'Color',[.13 .13 .13]);
% set camera line of sight (elevation = 90° - magic angle)
view(H,45,35.2644);
%% set up cubes
% half edge length of each cube
c = 0.5;
% movement vector (distance increases sigmoidally)
m = [0, diff(rescale(1./(1+exp(linspace(-10,10,16).*-1)))) ];
% get starting centerpoint coordinates for all cubes
cX = R((-1:1)',1,3,3);
cY = O(cX,[2 1 3]);
cZ = O(cX,[3 2 1]);
cPt = AF(@(x,y,z) [x y z],cX,cY,cZ,U,0);
% get linear idxs for each set of 9 cubes
% -x, +x
nx = F(cX<0); px = F(cX>0);
% -y, +y
ny = F(cY<0); py = F(cY>0);
% -z, +z
nz = F(cZ<0); pz = F(cZ>0);
% set up rotations to randomly scramble the cubes
% rotation axes for each cube
A = CF(@(e) e(:,randi(3)),R({ID},27,1),U,0);
% rotation angle for each cube
T = CF(@(C) C*randi(3),R({linspace(90,0,16)},27,1),U,0);
% patch object for each cube
cP = gobjects(3,3,3);
for i = 1:27
cP(i) = patch(H,...
'Faces',[1 2 3 4;1 2 6 5;1 4 8 5;5 6 7 8;3 4 8 7;2 3 7 6],...
'Vertices',cV(cPt{i}),...
'FaceVertexCData',[1 1 1;0 .61 .28;.72 .07 .2;1 .84 .01;0 .27 .68;1 .35 0],...
'FaceColor','flat',...
'EdgeColor',[0 0 0],...
'EdgeAlpha',1,...
'LineWidth',2);
end
end
% step (1-6)
S = ceil(f/16);
% frame step idx (1-16 per step)
fS = mod(f-1,16)+1;
% indices to cubes we are moving
ni = {nx;ny;nz};
pi = {px;py;pz};
% movement step
mS = mat2cell(ID.*m(fS),[1 1 1],3);
% % theta step idx (1-16)
tS = fS;
switch S
case {2 5}
mS = {};
case {3 6}
mS = CF(@(k) -k,mS,U,0);
end
switch S
case {1 6}
tS = 1;
case {3 4}
tS = 16;
case 5
tS = 17-tS;
end
%% translations
if ~isempty(mS)
% for each movement step
for i = 1:numel(mS)
% indices to cubes we are moving
idx = cell2mat([ni(i);pi(i)]);
% adjust centers
nP = [pM(mS{i},cPt(ni{i})); pP(mS{i},cPt(pi{i}))];
% get new vertices
nV = CF(@(pt) cV(pt),nP,U,0);
% update cube patches
set(cP(idx),{'Vertices'},nV);
% update the points
cPt(idx) = nP;
end
end
%% rotations
Tt = T;
if tS==16
Tt = CF(@(t) zeros(1,16)+t(end),T,U,0);
end
% rotate cube vertices, update patches
set(cP,{'Vertices'},AF(@(i) RV(i,A{i},Tt{i}(tS)),(1:27)',U,0))
drawnow;
% get patch Vertices for the cube using its centerpoint coordinates
function V = cV(P)
V = [c c c;-c c c;-c -c c;c -c c;c c -c;-c c -c;-c -c -c;c -c -c]+P;
end
% rotate cube vertices
function rV = RV(ci,u,rT)
% get cube centerpoint from its idx (ci)
P = cPt{ci};
% get cube vertices from its centerpoint
rV = cV(P);
% rotation matrix for any arbitrary axis, u
rM = cosd(rT) * ID + sind(rT) * [0 -u(3) u(2); u(3) 0 -u(1); -u(2) u(1) 0] + (1 - cosd(rT)) * (u * u');
% premultiply vectors for each cube vertex with the rotation matrix
for vi = 1:length(rV)
rV(vi,:) = (rM*(rV(vi,:)-cPt{ci}).').'+cPt{ci};
end
end
end
Movie
Audio
This submission does not have audio.