• Remix
  • Share
  • New Entry

on 14 Nov 2023
  • 24
  • 46
  • 0
  • 0
  • 1587
drawframe(1);
Write your drawframe function below
function drawframe(n)
persistent randmat H x y z
if n==1
[x,y,z]=heart(15,15);
ax=gca;hold on;view(3);camlight left;
ax.XLim=[-2,2];ax.YLim=[-2,2];ax.ZLim=[-2,2];
ax.DataAspectRatio=[1,1,1];
ax.Color=[0,0,0];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';
prop={'EdgeColor','none', 'FaceColor',[1,0,0]};
x=[x,x(:,1);x(1,:),x(1,1)];
y=[y,y(:,1);y(1,:),y(1,1)];
z=[z,z(:,1);z(1,:),z(1,1)];
for i=1:size(x,1)-1
for j=1:size(x,2)-1
H{i,j}=surf(x(i:i+1,j:j+1),...
y(i:i+1,j:j+1),z(i:i+1,j:j+1),prop{:});
end
end
randmat=rand([size(x),3]);
end
for ii=1:size(x,1)-1
for jj=1:size(x,2)-1
H{ii,jj}.XData=x(ii:ii+1,jj:jj+1)+n.*mean(mean(x(ii:ii+1,jj:jj+1))).*(.5+randmat(ii,jj,1))./48;
H{ii,jj}.YData=y(ii:ii+1,jj:jj+1)+n.*mean(mean(y(ii:ii+1,jj:jj+1))).*(.5+randmat(ii,jj,2))./48;
H{ii,jj}.ZData=z(ii:ii+1,jj:jj+1)+n.*mean(mean(z(ii:ii+1,jj:jj+1))).*(.5+randmat(ii,jj,3))./48;
end
end
function [cordX, cordY, cordZ] = heart(sizeTheta, sizeFai)
% HEART function generates the x,y,z coordinates for the heart shape.
% it's usage is quite similar to the SPHERE function in Matlab, but do not draw anything.
% the default size of x,y,z coordinates is [30, 160]
% the parameter of sizeFai is actually a quater of the whole range of Fai.
% the mathematical equation used here is found on web page
% http://mathworld.wolfram.com/HeartSurface.html
% This program is for free, i.e. you can copy, modify or distribute it for any usage.
% And I perfer you use it to express love to your special one :-)
% by Xin Zhao
% Feb 14, 2008
theta = linspace(0, pi, sizeTheta)';
nudge = 0.0001; % used to avoid the operlapping
fai = linspace(0 + nudge, pi/2 - nudge, round(sizeFai/4));
a = 9/4;
b = 9/80;
A = 1+(a-1)*(sin(theta).^2) * (sin(fai).^2);
B = (sin(theta).^2.*cos(theta).^3) * (1 + (b-1)*(sin(fai).^2));
rou = zeros(size(A));
for iLoop = 1:numel(A)
curA = A(iLoop);
curB = B(iLoop);
% this is the polar coordinates version of the sextic equation found on
% http://mathworld.wolfram.com/HeartSurface.html
polyFactors = [curA^3, -curB, -3*curA^2, 0, 3*curA, 0, -1];
solutions = roots(polyFactors);
realRou = real(solutions(abs(imag(solutions))< 1e-9));
rou(iLoop) = realRou(realRou>0);
end
% x,y,z for the quater of whole heart
xx = rou .* (sin(theta) * cos(fai));
yy = rou .* (sin(theta) * sin(fai));
zz = rou .* (cos(theta) * ones(size(fai)));
% x,y,z for the whole heart
cordX = [xx, -fliplr(xx) , -xx, fliplr(xx)];
cordY = [yy, fliplr(yy) , -yy, -fliplr(yy)];
cordZ = [zz, fliplr(zz), zz, fliplr(zz)];
end
end
Animation
Remix Tree