- /
- 
        Snake Toy
        on 16 Nov 2023
        
        
 
    - 38
- 93
- 0
- 4
- 1101
Snake Toy Animation Origins
The two seconds allowed here is just too few to make something compelling out of this toy here.
This is adapted from a snake toy animation we posted on the MATLAB social media feed back in June.  You can check that version out which had many more frames available to it (and some music).  (Is posting a link to a video cheating?)
https://twitter.com/MATLAB/status/1667109333333598208
function drawframe(f)
    %% Core Matrices
    numblock=24;
    v = [ -1 -1 -1 ; 1 -1 -1 ; -1  1 -1 ; -1  1  1 ; -1 -1  1 ; 1 -1  1 ];
    pf = [ 1 2 3 nan; 5 6 4 nan; 1 2 6 5; 1 5 4 3; 3 4 6 2 ];
    clr = hsv(numblock);
    % Left in a few options for anyone interested in remixing other shapes
    % and colors
    %n = pi/2;
    shapes = [ 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 % box
               %0 0 .5 -.5 .5 0 1 0 -.5 .5 -.5 0 1 0 .5 -.5 .5 0 1 0 -.5 .5 -.5 0 % fluer
               %0 0 1 1 0 .5 -.5 1 .5 .5 -.5 -.5 1 .5 .5 -.5 -.5 1 .5 .5 -.5 -.5 1 .5 % bowl
               %0 1 0 0 0 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 0 1 % dog
               %0 1 0 0 0 0 0 1 1 0 .5 0 1 1 0 1 1 0 -.5 0 1 1 0 0 % chicken
               %0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 % filled box
               %0 1 -n 0 0 0 1 n n 0 1 0 0 0 n 0 1 1 0 1 1 0 n 0 % cobra
               0 .5 -.5 -.5 .5 -.5 .5 .5 -.5 .5 -.5 -.5 .5 -.5 .5 .5 -.5 .5 -.5 -.5 .5 -.5 .5 .5]; % ball
    % Helper for making transform matrices.
    xform=@(R)makehgtform('axisrotate',[0 1 0],R,'zrotate',pi/2,'yrotate',pi,'translate',[2 0 0]);
    if f==1
        %% Create a neon type snake toy on a black background
        set(gcf,'color','black');
        axes('position',[0 0 1 1],'visible','off')
        P=hgtransform('Parent',gca,'Matrix',makehgtform('xrotate',pi*.5,'zrotate',pi*-.8));
        for i = 1:numblock
            P = hgtransform('Parent',P,'Matrix',xform(shapes(end,i)*pi));
            patch('Parent',P, 'Vertices', v, 'Faces', pf, 'FaceColor',clr(i,:),'EdgeColor','none');
            patch('Parent',P, 'Vertices', v*.75, 'Faces', pf(end,:), 'FaceColor','none',...
                  'EdgeColor','w','LineWidth',2);
        end
        %% Axes setup
        daspect([1 1 1]);
        view([10 60]);
        axis tight vis3d off
        camlight
    end
    % Get our stack of transforms.  These will magically be in the right order.
    h=findobj('type','hgtransform')';
    h=h(2:end); % Skip the first one
    % Orbit once around
    view([-f*360/48 20]);
    % Script Steps (transform there and back again)
    if f<=5
        return
    elseif f<=41
        steps=35;
        r=shapes(end,:)*pi; % Start at the Ball shape
        sh=shapes(1,:)*pi; % Go to the box shape
        s=f-6;
        % Transform to next step
        df = (sh-r)/steps;
        arrayfun(@(tx)set(h(tx),'Matrix',xform(r(tx)+df(tx)*s)),1:numblock);
    end
end


 
 
           