乐高小视频:使用matlab绘制“I heart you”

来源:百度文库 编辑:中财网 时间:2024/04/28 15:46:57
※※※“庆圣诞 迎元旦”六维空间第二届K歌大赛,点击报名~※※※

[其他种种] 使用matlab绘制“I heart you”

本帖最后由 Eastsun 于 2010-11-12 13:19 编辑

下载 (59.03 KB)
2010-11-11 19:16
下面是绘制这个心形的matlab代码:
  1. % Initialize the volume data, figure, and axes:
  2. [X,Y,Z] = meshgrid(linspace(-3,3,101));
  3. F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;
  4. hFigure = figure;
  5. sz = get(hFigure, 'Position');
  6. set(hFigure, 'Position', [sz(1)-0.15*sz(3) sz(2) 1.3*sz(3) sz(4)]);
  7. set(hFigure,'color','w', 'menu','none')

  8. hAxes = axes('Parent',hFigure,'NextPlot','add',...
  9.     'DataAspectRatio',[1 1 1],...
  10.     'XLim',[30 120],'YLim',[35 65],'ZLim',[30 75]);
  11. view([-39 30]);
  12. axis off

  13. % hidden surface removal
  14. hidden on

  15. % Fill the inside of the mesh with an isosurface to
  16. % block rendering of the back side of the heart
  17. p = patch(isosurface(F,-0.001));
  18. set(p,'FaceColor','w','EdgeColor','none');

  19. % Create and plot contours in the y-z plane:
  20. for iX = [35 38 41 45 48 51 54 57 61 64 67]
  21.     plane = reshape(F(:,iX,:),101,101);
  22.     cData = contourc(plane,[0 0]);
  23.     xData = iX.*ones(1,cData(2,1));
  24.     plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'k');
  25.     pause(.1), drawnow
  26. end

  27. % Create and plot contours in the x-z plane:
  28. for iY = [41 44 47 51 55 58 61]
  29.     plane = reshape(F(iY,:,:),101,101);
  30.     cData = contourc(plane,[0 0]);
  31.     yData = iY.*ones(1,cData(2,1));
  32.     plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'k');
  33.     pause(.1), drawnow
  34. end
复制代码

再来个复杂点的:
下载 (46.76 KB)
2010-11-11 19:17
相关代码鼠标右键点击下面的图片,通过“图片另存为”保存到电脑上。然后将这个图片文件的后缀名修改为"rar"(即将.jpg修改为.rar)。然后双击打开,里面有一个“iHeart.m”文件,解压之。这样通过matlab来运行这个iHeart函数就可以了。(注意:有同学说看不到下面的图片或解压出错,我在76楼重新上传了rar文件,内含代码

来一个动画版的,作者是Xin Zhao(在matlab上运行才能看到效果,相当棒):
下载 (72.61 KB)
2010-11-11 19:19
呵呵,刚刚学会了怎么讲matlab动画保存为gif,下面是实际效果:
下载 (3.53 MB)
2010-11-11 23:26
代码可以从我上面提供的链接下载,或者将下面的图片保存为rar文件,解压可得。



这个是为我GF做的:
※※※“庆圣诞 迎元旦”六维空间第二届K歌大赛,点击报名~※※※

[其他种种] 使用matlab绘制“I heart you”

本帖最后由 Eastsun 于 2010-11-12 13:19 编辑

下载 (59.03 KB)
2010-11-11 19:16
下面是绘制这个心形的matlab代码:
  1. % Initialize the volume data, figure, and axes:
  2. [X,Y,Z] = meshgrid(linspace(-3,3,101));
  3. F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;
  4. hFigure = figure;
  5. sz = get(hFigure, 'Position');
  6. set(hFigure, 'Position', [sz(1)-0.15*sz(3) sz(2) 1.3*sz(3) sz(4)]);
  7. set(hFigure,'color','w', 'menu','none')

  8. hAxes = axes('Parent',hFigure,'NextPlot','add',...
  9.     'DataAspectRatio',[1 1 1],...
  10.     'XLim',[30 120],'YLim',[35 65],'ZLim',[30 75]);
  11. view([-39 30]);
  12. axis off

  13. % hidden surface removal
  14. hidden on

  15. % Fill the inside of the mesh with an isosurface to
  16. % block rendering of the back side of the heart
  17. p = patch(isosurface(F,-0.001));
  18. set(p,'FaceColor','w','EdgeColor','none');

  19. % Create and plot contours in the y-z plane:
  20. for iX = [35 38 41 45 48 51 54 57 61 64 67]
  21.     plane = reshape(F(:,iX,:),101,101);
  22.     cData = contourc(plane,[0 0]);
  23.     xData = iX.*ones(1,cData(2,1));
  24.     plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'k');
  25.     pause(.1), drawnow
  26. end

  27. % Create and plot contours in the x-z plane:
  28. for iY = [41 44 47 51 55 58 61]
  29.     plane = reshape(F(iY,:,:),101,101);
  30.     cData = contourc(plane,[0 0]);
  31.     yData = iY.*ones(1,cData(2,1));
  32.     plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'k');
  33.     pause(.1), drawnow
  34. end
复制代码

再来个复杂点的:
下载 (46.76 KB)
2010-11-11 19:17
相关代码鼠标右键点击下面的图片,通过“图片另存为”保存到电脑上。然后将这个图片文件的后缀名修改为"rar"(即将.jpg修改为.rar)。然后双击打开,里面有一个“iHeart.m”文件,解压之。这样通过matlab来运行这个iHeart函数就可以了。(注意:有同学说看不到下面的图片或解压出错,我在76楼重新上传了rar文件,内含代码

来一个动画版的,作者是Xin Zhao(在matlab上运行才能看到效果,相当棒):
下载 (72.61 KB)
2010-11-11 19:19
呵呵,刚刚学会了怎么讲matlab动画保存为gif,下面是实际效果:
下载 (3.53 MB)
2010-11-11 23:26
代码可以从我上面提供的链接下载,或者将下面的图片保存为rar文件,解压可得。



这个是为我GF做的: