使用MATLAB绘制心心

  • TIT的校友在管家墙上发了各种python、JAVA、C、C+:heart:,小张也不甘示弱!!!

1.二维静态:heart:

f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
[x,y,z]=meshgrid(linspace(-3,3));
val=f(x,y,z);
[p,v]=isosurface(x,y,z,val,0);
patch('faces',p,'vertices',v,'facevertexcdata',jet(size(v,1)),'facecolor','w','edgecolor','flat');
view(3);
grid on;
axis equal;

image-20211206161211669

2.二维动画版

warning  off
for b=-10:1:40
clf;
x=[-1.6:0.01:1.6];
y=(sqrt(cos(x)).*cos(b.*200.*x)+sqrt(abs(x))-0.7).*(4-x.^2).^0.01;
plot(x,y,'r','LineWidth',2);
axis off
pause(0.3)
hold off
end

image-20211206165926649

3.三维动画版

[X,Y,Z] = meshgrid(linspace(-3,3,101));
F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;
hFigure = figure;
sz = get(hFigure, 'Position');
set(hFigure, 'Position', [sz(1)-0.15*sz(3) sz(2) 1.3*sz(3) sz(4)]);
set(hFigure,'color','w', 'menu','none')
hAxes = axes('Parent',hFigure,'NextPlot','add', 'DataAspectRatio',[1 1 1], 'XLim',[30 120],'YLim',[35 65],'ZLim',[30 75]);
view([-39 30]);
axis off
hidden on
p = patch(isosurface(F,-0.001));
set(p,'FaceColor','w','EdgeColor','none');
for iX = [35 38 41 45 48 51 54 57 61 64 67]
plane = reshape(F(:,iX,:),101,101);
cData = contourc(plane,[0 0]);
xData = iX.*ones(1,cData(2,1));
h3=plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'k');
set(h3,'color','r');
pause(.1), drawnow
end
for iY = [41 44 47 51 55 58 61]
plane = reshape(F(iY,:,:),101,101);
cData = contourc(plane,[0 0]);
yData = iY.*ones(1,cData(2,1));
h4=plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'k');
set(h4,'color','r');
pause(.1), drawnow
end
for iZ = [36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 69 71]
plane = F(:,:,iZ);
cData = contourc(plane,[0 0]);
startIndex = 1;
if size(cData,2) > (cData(2,1)+1)
startIndex = cData(2,1)+2;
zData = iZ.*ones(1,cData(2,1));
h0=plot3(hAxes,cData(1,2:(startIndex-1)),cData(2,2:(startIndex-1)),zData,'k');
set(h0,'color','r');
end
zData = iZ.*ones(1,cData(2,startIndex));
h1=plot3(hAxes,cData(1,(startIndex+1):end),cData(2,(startIndex+1):end),zData,'k');
set(h1,'color','r');
pause(.1), drawnow
end
pause(.2)
text(7,50,70,'I', 'color','r','fontWeight','bold','FontAngle','italic','fontsize',100)
pause(.5)
text(80,50,43,'YOU','color','r' ,'fontWeight','bold','FontAngle','italic','fontsize',100)
pause(.2)
text(50,65,90,'GC_Zhang', 'fontsize',38)
%line([20 80],[50 50],[52.5 52.5], 'color','k')
%line([50 50],[20 80],[52.5 52.5], 'color','k')
%line([50 50],[50 50],[30 80], 'color','k')

image-20211206161240131

4.同时播放

clc
clear all;
close all;
warning off
for b=-4:1:4
clf;
x=[-1.6:0.01:1.6];
y=(sqrt(cos(x)).*cos(b.*200.*x)+sqrt(abs(x))-0.7).*(4-x.^2).^0.01;
plot(x,y,'r','LineWidth',2);
axis off
pause(0.3)
hold off
end


[X,Y,Z] = meshgrid(linspace(-3,3,101));
F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;
hFigure = figure;
sz = get(hFigure, 'Position');
set(hFigure, 'Position', [sz(1)-0.15*sz(3) sz(2) 1.3*sz(3) sz(4)]);
set(hFigure,'color','w', 'menu','none')
hAxes = axes('Parent',hFigure,'NextPlot','add', 'DataAspectRatio',[1 1 1], 'XLim',[30 120],'YLim',[35 65],'ZLim',[30 75]);
view([-39 30]);
axis off
hidden on
p = patch(isosurface(F,-0.001));
set(p,'FaceColor','w','EdgeColor','none');
for iX = [35 38 41 45 48 51 54 57 61 64 67]
plane = reshape(F(:,iX,:),101,101);
cData = contourc(plane,[0 0]);
xData = iX.*ones(1,cData(2,1));
h3=plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'k');
set(h3,'color','r');
pause(.1), drawnow
end
for iY = [41 44 47 51 55 58 61]
plane = reshape(F(iY,:,:),101,101);
cData = contourc(plane,[0 0]);
yData = iY.*ones(1,cData(2,1));
h4=plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'k');
set(h4,'color','r');
pause(.1), drawnow
end
for iZ = [36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 69 71]
plane = F(:,:,iZ);
cData = contourc(plane,[0 0]);
startIndex = 1;
if size(cData,2) > (cData(2,1)+1)
startIndex = cData(2,1)+2;
zData = iZ.*ones(1,cData(2,1));
h0=plot3(hAxes,cData(1,2:(startIndex-1)),cData(2,2:(startIndex-1)),zData,'k');
set(h0,'color','r');
end
zData = iZ.*ones(1,cData(2,startIndex));
h1=plot3(hAxes,cData(1,(startIndex+1):end),cData(2,(startIndex+1):end),zData,'k');
set(h1,'color','r');
pause(.1), drawnow
end
pause(.2)
text(7,50,70,'I', 'color','r','fontWeight','bold','FontAngle','italic','fontsize',100)
pause(.5)
text(80,50,43,'YOU','color','r','fontWeight','bold','FontAngle','italic','fontsize',100)
pause(.2)
text(50,65,90,'CSDN 张时贰', 'color','c','fontsize',38)
%line([20 80],[50 50],[52.5 52.5], 'color','k')
%line([50 50],[20 80],[52.5 52.5], 'color','k')
%line([50 50],[50 50],[30 80], 'color','k')