- /
-
Dodecahedron Unrolled
on 18 Nov 2023
- 18
- 60
- 1
- 0
- 1805
drawframe(1);
Write your drawframe function below
function drawframe(f)
%% Polyhedra Data so we can vectorize
% The following polyhedra data can be downloaded from:
% https://netlib.org/polyhedra/3
% 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.
% Decoder
D=@(v,c)reshape(v-'0',numel(v)/c,c);
% Faces Array
F=D(';VJ52G@1=RULAQC4:N96DSMFHIB;AH?>ELF?IHIB;AF?>EL>BPO<3@G78KTE',5);
% Compress Doubles:
% Identify # of unique values. If that # is small, create reference
% array with the unique values. Then compress the indices into the
% array of unique values to recreate the original array
% If unique values can be represented as colonop easily, do that.
%
% Vertex Array
V=[-1.8017 -3.9271
-1.8017 -1.309
-1.8017 -0.30902
-1.8017 0.30902
-1.8017 1.309
-1.2139 -4.7361
-1.2139 -3.118
-0.85065 -5.2361
-0.85065 -2.618
-0.85065 -1.618
-0.85065 0
-0.85065 1.618
-0.26287 -6.0451
-0.26287 -4.4271
-0.26287 -3.4271
-0.26287 -1.809
-0.26287 -0.80902
-0.26287 0.80902
-0.26287 1.809
0.68819 -5.7361
0.68819 -4.7361
0.68819 -3.118
0.68819 -2.118
0.68819 -0.5
0.68819 0.5
0.68819 2.118
1.276 -5.5451
1.276 -3.9271
1.276 -2.309
1.276 -1.309
1.276 1.309
1.6392 -0.80902
1.6392 0.80902
2.227 -5.2361
2.227 -4.2361
2.227 -3.618
2.227 -2.618
2.227 0];
% Z is zero, so this is cheaper.
% Also, we'll transform the verts by hand, so need 4 column of ones
V(:,3)=0;
V(:,4)=1;
% Origin of faces so we can offset/fold
O=[0.68819 -0.5 0
0.68819 -0.5 0
0.68819 0.5 0
-0.26287 0.80902 0
-0.85065 0 0
0 0 0
-0.26287 -1.809 0
-0.26287 -3.4271 0
-0.26287 -4.4271 0
0.68819 -4.7361 0
1.276 -3.9271 0
-0.26287 -3.4271 0];
% Rotation Axis
R=[-0.95106 -0.30902 0
0 1 0
-0.95106 0.30902 0
-0.58779 -0.80902 0
0.58779 -0.80902 0
0 0 0
0.95106 -0.30902 0
0 -1 0
0.95106 -0.30902 0
0.58779 0.80902 0
-0.58779 0.80902 0
0.95106 0.30902 0];
% Angle of rotation for the solid
A=[0
2.0344];
A=A(D('222221222222',1));
% Children indices for each face to create the graph
C=D('500001<0000;20000700000830000000000940000000000:',4);
%% Fold factor
% 0 is wide open, 1 is fully solid
ff=(f-25)/24;
%% Build child graph using
persistent P TX
if f==1
axes('pos',[0 0 1 1],'clipping','off');
%% Place in transform b/c fold inversion is no symetric
TX=hgtransform;
%% We need a symetric set of face colors
% This is for the illusion of infinite refolding
fvc=[2
4
3
5
0
1 % root face
3
4
1 % face opposite root
0
5
2];
%% Create the patch that draws the polyhedra
P = patch(TX,'Faces', F, 'Vertices', V(:,1:3), 'FaceVertexCData', fvc, ...
'EdgeC', 'w','FaceC','f','LineW',2);
colormap(orderedcolors('dye'));
%% Make axes nice
daspect([1 1 1]);
axis('tight','off','vis3d')
view([80 30])
camzoom(1.2)
end
%% Backdrop
set(gcf,'color',[1 1 1]*abs(ff)*.25+.5);
%% Rotate
set(TX,'Matrix',makehgtform('zrotate',pi*(f-1)/48)); %A(1)*(f-1)/48));
%% 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(6);
set(P,'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.
fv = unique([fv F(fidx,:)]);
% 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 verts, and all child vertices.
o=[O(fidx,:)';0];
NV(:,fv)=M*(NV(:,fv)-o)+o;
end
end