Matlab绘制三维动态心形It’s OK to send a pic to your girlfriend on Valentine's Day情人节蛋疼玩意效果图:原始代码:%仅供参考,自助修改,原则上自己动手,要是非常强烈的要帮忙%可以联系我的QQ 865802870 ,但愿我还在上面.Source code:%构造体积方程和坐标轴,画出图形;linspace(a,b,c)均匀生成介于a到b的c个值,c 的默认为100。
Meshgrid生成矩阵网格。
[X,Y,Z] = meshgrid(linspace(-3,3,101));%3D心型图方程如下;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','w');% 构造Y-Z平面,,描完函数在该平面的点: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));plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'r');pause(.1), drawnowend% 构造X-Z平面,描完函数在该平面的点: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));plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'r');pause(.1), drawnowend% 构造X-Y平面,描完函数在该平面的点: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));plot3(hAxes,cData(1,2:(startIndex-1)),...cData(2,2:(startIndex-1)),zData,'r');endzData = iZ.*ones(1,cData(2,startIndex));plot3(hAxes,cData(1,(startIndex+1):end),...cData(2,(startIndex+1):end),zData,'r');pause(.1), drawnowend%给三维心着色set(p,'FaceColor','r','EdgeColor','w');pause(.2);set(p,'FaceColor','w','EdgeColor','r');%函数已经画完,接下来为文字部分;pause(.2)%设置字体大小,粗细,位置等,以下是打出I (心型图) Wendy;text(7,50,70,'I', 'fontWeight','bold','FontAngle','italic','FontName','Trebuchet MS','fontsize',60,'Color','r')pause(.5)text(80,50,43,'Math', 'fontWeight','bold','FontAngle','italic','FontName','Trebuchet MS','fontsize',60,'Color','r')pause(.2)line([20 80],[50 50],[52.5 52.5], 'color','r')line([50 50],[20 80],[52.5 52.5], 'color','r')line([50 50],[50 50],[30 80], 'color','r')%制作者签名;text(40,60,30,'Made By William 8/8/2012', 'fontsize',8)text(35,45,30,'', 'fontsize',8)%制作心的动态效果% for i =1:28% set(p,'FaceColor','r','EdgeColor','w');% pause(.1);% set(p,'FaceColor','r','EdgeColor','r');% pause(.2)% end% refer to /art/898961...%and/questions/1526898/how-do-i-reproduce-this-heart-shaped-m esh-in-matlab1%笛卡尔;x=0:0.01:2*pi;y=1-cos(x);polar(x,y)2%简单心;figure(2)ezplot(vectorize('17*x^2-16*abs(x)*y+17*y^2-225'));3%完美心;4figure(1)5N=200;6f1=@(x,y,z)(x.^2+(9/4)*y.^2+z.^2-1).^3-x.^2.*z.^3-(11/80)*y.^2.*z.^3;7[X,Y,Z]=meshgrid(linspace(-1.5,1.5,N));8set(patch(isosurface(X,Y,Z,f1(X,Y,Z),0)),'facecolor','r','edgecolor','none');9light10view(-10,24)11%心痕;12[x,y,z]=meshgrid(linspace(-1.3,1.3));13val=(x.^2 + (9/4)*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (1/9)*y.^2.*z.^3;%画出等值面14isosurface(x,y,z,val,0);% View 视角colormap 色图0-115axis equal;view(-10,10);colormap([1 0.2 0.2])16%花心;17clear;clc;close all18c=5;19t=linspace(-c,c);20[x,y]=meshgrid(t);21z=17*x.^2-16*abs(x).*y+17*y.^2-225;22pcolor(x,y,z);23shading interp24pause(2);25spinmap(10)26%构造体积方程和坐标轴,画出图形;27[X,Y,Z] = meshgrid(linspace(-3,3,101));2829%3D心型图方程如下;30 F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;31hFigure = figure;32sz = get(hFigure, 'Position');33set(hFigure, 'Position', [sz(1)-0.15*sz(3) sz(2) 1.3*sz(3) sz(4)]); 34set(hFigure,'color','w', 'menu','none')3536hAxes = axes('Parent',hFigure,'NextPlot','add',...37'DataAspectRatio',[1 1 1],...38'XLim',[30 120],'YLim',[35 65],'ZLim',[30 75]);39view([-39 30]);40axis off4142% 制作出动态的隐形效果;4344hidden on4546% 画出网格,制作网格动态效果;474849% 快渲染心得背面:5051p = patch(isosurface(F,-0.001));52set(p,'FaceColor','w','EdgeColor','w');5354% 构造Y-Z平面,,描完函数在该平面的点:55for iX = [35 38 41 45 48 51 54 57 61 64 67]56plane = reshape(F(:,iX,:),101,101);57cData = contourc(plane,[0 0]);58xData = iX.*ones(1,cData(2,1));59plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'k');60pause(.1), drawnow61end6263% 构造X-Z平面,描完函数在该平面的点:64for iY = [41 44 47 51 55 58 61]65plane = reshape(F(iY,:,:),101,101);66cData = contourc(plane,[0 0]);67yData = iY.*ones(1,cData(2,1));68plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'k');69pause(.1), drawnow70end7172% 构造X-Y平面,描完函数在该平面的点:73for iZ = [36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 69 71] 74plane = F(:,:,iZ);75cData = contourc(plane,[0 0]);76startIndex = 1;77if size(cData,2) > (cData(2,1)+1)78startIndex = cData(2,1)+2;79zData = iZ.*ones(1,cData(2,1));80plot3(hAxes,cData(1,2:(startIndex-1)),... 81cData(2,2:(startIndex-1)),zData,'k');82end83zData = iZ.*ones(1,cData(2,startIndex)); 84plot3(hAxes,cData(1,(startIndex+1):end),... 85cData(2,(startIndex+1):end),zData,'k');86pause(.1), drawnow87end。