• Remix
  • Share
  • New Entry

on 18 Nov 2023
  • 19
  • 35
  • 0
  • 0
  • 1929
drawframe(1);
Write your drawframe function below
function drawframe(f)
% The following polyhedra data can be downloaded from:
% https://netlib.org/polyhedra/14
% And was pre-processed into these compressed arrays.
% For Positive Integers:
% Convert to char, and offset into the printable character range by
% Adding something like '0' to it.
% 0 is convenient as you know what the first 10 #s are by sight.
% For faces array, nan become 0 and need to be converted back.
%
% Decoder
D=@(v,c)reshape(v-'0',numel(v)/c,c);
% Faces Array
F=D('654@?>ONM^]\jihtsr~}|ˆ‡†PL523<>;NELX\Wicgpro}w{„†ƒHB917?D=TDR]b[mbksvqv‡Š…GA:48EECUMSccanhlwwu‚|€‹‹‰OK080J0I0S0f0e0l0z0y0€0Ž0UQ0>0F0D0\0d0b0r0x0v0†0Œ0Š_Y0?00000]00000s00000‡0000`Z0900000T00000m000000000VR',8);
F(F==0)=nan;
% Compress Doubles:
% Identify # of unique values. If that # is small, create reference
% array with the unique values.
% Scale unique values into 16bit unicode chars and save that,
% so rescale them to decode
% Then compress the indices into the
% array of unique values to recreate the original array
%
% Vertex Array
V=rescale('0Ӡ஁ဇဲᗅᛓᮃ∤⛔⟢⵶ⶠ⻕㈦㣇㰘㵷䐘䕸䣉佪劺吚媻尛彫昍楝檽煞犽瘎粯耀'-'0',-6.3284,12.9497);
V=V(D('99::::<<<<>>????@@@@@@@@AABBBBBBCCCCCCDDEEEEEEEEFFFFGGHHHHIIIIJJKKKKLLLLMMNNNNOOOOPPQQQQRRRRSS8957:<57:<6;489=23489=?@6;157:<B157:<B6;23489=?@489=6;57:<57:<6;489=489=6;57:<57:<6;489=489=6;',2));
V(:,3)=0;
V(:,4)=1;% Origin of faces so we can offset/fold
O=rescale('0ࠀඅᕕ᫛ể⊪⠰ 㖆㵕䋛䪪倰堀嶆敕櫛犫砰耀'-'0',-3.9142,12.4497);
O=O(D('785998;6:=<<?=>A@@CABEDD;:54244356243353243353243371',2));
O(:,3)=0;
% Rotation Axis
R=[-1
0
1];
R=R(D('1231231231231231231231231321221222223223223223223222',2));
R(:,3)=0;
% Angle of rotation for the solid
A=[0
2.3562
2.5261];
A=A(D('22232321232322232322232322',1));
% Children indices for each face to create the graph
C=D('010040I7J0:00=00@00C00F0000300200900<00?00B00E00H0000000600;00>00A00D00G00000000000005000000000000000000',4);
%% Fold factor
% 0 is wide open, 1 is fully solid
ff=(f-25)/24;
%% Build child graph using
persistent TX1 TX2 P1 P2
if f==1
axes('pos',[0 0 1 1],'clipping','off');
%% We need a symetric set of face colors
% This is for the illusion of infinite refolding
fvc=sum(~isnan(F),2)-2;
%% Create the patch that draws the polyhedra
TX1=hgtransform;
TX2=hgtransform;
pf=@(p)patch(p,'Faces', F, 'Vertices', V(:,1:3), 'FaceVertexCData', fvc, ...
'CDataMapping','direct','EdgeC', 'w','FaceC','f','LineW',2);
P1=pf(TX1);
P2=pf(TX2);
colormap(orderedcolors('earth'));
%% Make axes nice
daspect([1 1 1]);
axis('tight','off','vis3d')
view([-120 30])
end
%% Recenter vertices so scaling looks nice
ctr=mean(V(F(8,:),:),1);
V=V-ctr;
O=O-ctr(1,1:3);
%% Backdrop
set(gcf,'color',[1 1 1].*[abs(ff)*.5+.4 .5 1]);
%% Fold
% Starting at our root face, recurse down and move the vertices around.
% I generated the data with facde 6 as root
NV=V';
recursiveFold(8);
set(P1,'Vertices',NV(1:3,:)');
if ff==0
set(TX2,'visible','off');
else
set(TX2,'visible','on',...
'Matrix',makehgtform('zrotate',ff*pi*1.5, ...
'translate',[0 0 -ff],...
'scale',abs(ff)));% Shrink away to 0
end
NV=V';
if ff<0 % Place on opposite side
ff=1;
else
ff=-1;
end
recursiveFold(8);
set(P2,'Vertices',NV(1:3,:)');
function fv = recursiveFold(fidx)
fv = [];
% Recurse into children first to get their folded vertex locations
% because we need to fold them in addition to our own (as if in a
% nested transform.)
for i=1:sum(C(fidx,:)>0)
% This returns all vertices affected and collects them
fv = [fv recursiveFold(C(fidx,i))]; %#ok
end
% Add ourselves into the list of vertices being folded at this step.
% Make sure the list is unique though.
l=F(fidx,:);
fv = unique([fv l(~isnan(l))]);
% If there is no angle to fold to, then exit.
if A(fidx)==0
return
end
M=makehgtform('axisrotate', R(fidx,:),ff*(A(fidx)-pi));
% Transform our and all child vertices.
% TX * verts, since verts has N columns
o=[O(fidx,:)';0];
NV(:,fv)=M*(NV(:,fv)-o)+o; % rotate and then move back
end
end
Animation
Remix Tree