• Remix
  • Share
  • New Entry

on 14 Nov 2023
  • 12
  • 36
  • 1
  • 0
  • 1123
drawframe(1);
Write your drawframe function below
function drawframe(f)
% function Bouncing_Bucky(f)
% Bouncing_Bucky, no arguments, random self-initializing infinite loop.
% Bouncing_Bucky(1), initialize and take one step.
% Bouncing_Bucky(f), f > 1, take one step.
fps = 24; % Frames per second
if nargin == 0 || f == 1
% Initialize
cla
axis([0 1 0 1])
axis square
box on
set(gca,'xtick',[],'ytick',[])
% Adjacency matrix and vertices of truncated icosahedron
[B,V] = bucky;
V = V/8;
% Color pentagons and hexagons
blue_gray = 0.9*[0 0 .5; 1 1 1];
C = allcycles(graph(B), maxCycleLength = 6);
L = cellfun(@length, C);
F = NaN(height(C), max(L));
for k = 1:height(C)
F(k,1:L(k)) = C{k};
end
FaceColor = blue_gray((L == max(L))+1, :);
% Handle Graphics Transform object.
H = hgtransform(Parent = gca);
% Patches
patch(Parent = H, ...
Faces = F, ...
Vertices = V, ...
EdgeColor = blue_gray(1,:), ...
FaceVertexCData = FaceColor, ...
FaceColor = 'flat')
% Initial position {x,y,dx,dy}
if nargin > 0
initial = {0.5, 0, 1/fps, 1/fps};
else
initial = {rand, rand, randn/fps, randn/fps};
end
set(gca,'userdata',initial)
end
while 1
% One step
g = get(gca,'userdata');
[x,y,dx,dy] = deal(g{:});
x = x + dx;
y = y + dy;
if x < 0 || x > 1
dx = -dx;
end
if y < 0 || y > 1
dy = -dy;
end
% Transform matrix with fake rotation
beta = 90*(2*x+y);
M = Ry(beta) * Rx(2*beta);
M(1:2,4) = [x;y];
H = get(gca,'children');
H.Matrix = M;
% Save position for next frame
set(gca,'userdata',{x,y,dx,dy})
if nargin > 0
break
else
% ==> pause(1/fps)
end
end
% -------------------------------------------------------
function R = Rx(t)
R = [1 0 0 0
0 cosd(t) sind(t) 0
0 -sind(t) cosd(t) 0
0 0 0 1];
end
function R = Ry(t)
R = [cosd(t) 0 sind(t) 0
0 1 0 0
-sind(t) 0 cosd(t) 0
0 0 0 1];
end
end
Animation
Remix Tree