Search code examples
matlabmatlab-figure

Make the heart move up/down/left/right


I was able to make a heart in matlab as:

n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
isosurface(F,0)
lighting phong
axis equal

Let's say I would like to move it up just a moment (i.e. 1s) after showing the figure. I mean, 1st step is to show the heart and 2nd step is to move it ,i.e. up (or down/left/right). What approach could be taken?


Solution

  • you can redraw your isosurface with new coordinates after 1 second

    n=100;
    x=linspace(-3,3,n);
    y=linspace(-3,3,n);
    z=linspace(-3,3,n);
    [X,Y,Z]=ndgrid(x,y,z);
    F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
    
    [XX, YY, ZZ] = meshgrid(x,y,z);
    isosurface(XX,YY,ZZ,F,0);
    
    lighting phong
    axis equal
    axis manual
    ax = gca;
    k = 2;
    
    ax.XLim = ax.XLim*k;
    ax.YLim = ax.YLim*k;
    ax.ZLim = ax.ZLim*k;
    
    pause(1);
    
    TranslationVector = [0.5 0.5 0.5];
    
    h = findobj('Parent',ax,'type','patch');
    delete(h);
    isosurface(XX+TranslationVector(1),YY+TranslationVector(2),ZZ+TranslationVector(3),F,0)