- /
- 
        Expanding Cube
        on 30 Oct 2024
        
        
 
    - 32
- 219
- 0
- 0
- 1842
 Cite your audio source here (if applicable): 
drawframe(1);
 Write your drawframe function below
function drawframe(f)
    persistent hAx cubePatch nxi pxi nyi pyi nzi pzi cP c m nP
    % setup
    if f==1
        %% helper functions
        nP = @(a,b) cellfun(@(pt) pt.*a,b,'UniformOutput',false);
        %% figure/axes
        % store axes handle
        hAx = gca();
        % set axes props
        set(hAx,...
            'Visible','off',...
            'Units','Normalized',...
            'InnerPosition',[0 0 1 1],...
            'XLim',[-3 3],...
            'YLim',[-3 3],...
            'ZLim',[-3 3],...
            'NextPlot','Add',...
            'DataAspectRatio',[1 1 1],...
            'PlotBoxAspectRatio',[1 1 1]);
        % set figure backgroud color
        set(gcf,'Color',[.13 .13 .13]);
        %% cube properties
        % half length of each cube edge
        c = 0.5;
        % Faces
        F = zeros(6,4);
        % define 6 faces of the cube
        F(1,:) = [1,2,3,4]; % +z
        F(2,:) = [1,2,6,5]; % +y
        F(3,:) = [1,4,8,5]; % +x
        F(4,:) = [5,6,7,8]; % -z
        F(5,:) = [3,4,8,7]; % -y
        F(6,:) = [2,3,7,6]; % -x
        % FaceVertexCData
        c1 = [.99,.84,.29]; % z face
        c2 = [.90,.63,.17]; % y face
        c3 = [.76,.45,.18]; % x face
        FVC = repmat([c1;c2;c3],2,1);
        % movement scaling vector (distance increases sigmoidally)
        m = rescale(1./(1+exp(linspace(-10,10,16).*-1)));
        % get starting center coordinates for all cubes
        cX = repmat((-1:1)',1,3,3);
        cY = permute(cX,[2,1,3]);
        cZ = permute(cX,[3,2,1]);
        cP = arrayfun(@(x,y,z) [x,y,z],cX,cY,cZ,'UniformOutput',false);
        % get linear idxs for each set of 9 cubes
        nxi = find(cX<0); % -x
        pxi = find(cX>0); % +x
        nyi = find(cY<0); % -y
        pyi = find(cY>0); % +y
        nzi = find(cZ<0); % -z
        pzi = find(cZ>0); % +z
        %% build patch objects for each cube
        cubePatch = gobjects(3,3,3);
        for x = 1:3
            for y = 1:3
                for z = 1:3
                    cubePatch(x,y,z) = patch(hAx,...
                        'Faces',F,...
                        'Vertices',cubeVertices(cP{x,y,z}),...
                        'FaceVertexCData',FVC,...
                        'FaceColor','flat',...
                        'EdgeAlpha',0);
                end
            end
        end
        % set camera line of sight (magic angle elevation)
        view(hAx,45,35.2644);
    end
    step = ceil(f/16);
    fStep = mod(f-1,16)+1;
    % get patch data for next frame of current step
    switch step
        case 1 % expand x
            % indices to cubes we are moving
            idx = [nxi;pxi];
            % scale factor 
            sF = [m(fStep)+1,1,1];
        case 2 % expand y
            % indices to cubes we are moving
            idx = [nyi;pyi];
            % scale factor
            sF = [1,m(fStep)+1,1];
        case 3 % expand z
            % indices to cubes we are moving
            idx = [nzi;pzi];
            % scale factor 
            sF = [1,1,m(fStep)+1];
        case 4 % contract x
            % indices to cubes we are moving
            idx = [nxi;pxi];
            % scale factor
            sF = [m(fStep)/2-1,1,1];
        case 5 % contract y
            % indices to cubes we are moving
            idx = [nyi;pyi];
            % scale factor
            sF = [1,m(fStep)/2-1,1];
        case 6 % contract z
            % indices to cubes we are moving
            idx = [nzi;pzi];
            % scale factor
            sF = [1,1,m(fStep)/2-1];
    end
    % get new cube center coordinates
    newP = nP(sF,cP(idx));
    % get new vertices
    newV = cellfun(@(pt) cubeVertices(pt),newP,'UniformOutput',false);
    % update cube patches
    set(cubePatch(idx),{'Vertices'},newV);
    % at the end of each step, update the center coordinates
    if fStep==16
        cP(idx) = newP;
    end
    drawnow;
    % get patch Vertices (V) for a cube from its center point (P)
    function V = cubeVertices(P)
        pX = P(1);
        pY = P(2);
        pZ = P(3);
        % Vertices
        V = zeros(8,3);
        % top face (positive z)
        V(1,:) = [pX+c,pY+c,pZ+c];
        V(2,:) = [pX-c,pY+c,pZ+c];
        V(3,:) = [pX-c,pY-c,pZ+c];
        V(4,:) = [pX+c,pY-c,pZ+c];
        % bottom face (negative z)
        V(5,:) = [pX+c,pY+c,pZ-c];
        V(6,:) = [pX-c,pY+c,pZ-c];
        V(7,:) = [pX-c,pY-c,pZ-c];
        V(8,:) = [pX+c,pY-c,pZ-c];
    end
end
Movie
Audio
This submission does not have audio.


 

