- /
-
Bouncing Ball Illusion (simplified)
on 13 Nov 2023
- 11
- 117
- 0
- 0
- 983
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent pgon
if f == 1
vq = -1:2/12:1;
vqL = length(vq);
pgon = polyshape([vq ones(1,vqL-1) -vq(2:end) -ones(1,vqL-1)], ...
[ones(1,vqL) -vq(2:end) -ones(1,vqL-1) vq(2:end)],'KeepCollinearPoints', true);
% len = length(pgon.Vertices);
end
len = 48;
col = [0.3020 0.7451 0.9333];
t=0:pi/50:2*pi;
a = sqrt(2)+0.05;
dth = 5;
l = f;
cla
hold on
alpha = max(0.5 - abs((l+len/4)/len-1)*2, 0);
for th = 0:dth:45
r = rotate(pgon,th);
idx = mod(l + th/dth*2, len) + 1;
if th==45
col2 = [col(1)+(1-col(1))*(alpha/0.7) col(2)*(1-alpha/0.7) col(3)*(1-alpha/0.7)];
plot(r.Vertices(idx,1), r.Vertices(idx,2), 'o', MarkerSize=10, ...
MarkerFaceColor=col2, MarkerEdgeColor=col2);
else
alpha2 = max((1 - alpha*3), 0);
col2 = col * alpha2 + [1 1 1] * (1-alpha2);
plot(r.Vertices(idx,1), r.Vertices(idx,2), 'o', MarkerSize=10, ...
MarkerFaceColor=col2, MarkerEdgeColor=col2);
end
if th==45
wid = 1.5;
boxCol = 'r';
plot(r, EdgeColor=boxCol, EdgeAlpha=alpha, FaceColor='w', FaceAlpha=0, LineWidth=wid);
end
plot(a*cos(t), a*sin(t), 'r', LineWidth=2)
end
axis off
axis(gca,'equal')
hold off
end