MATLAB之心形图绘制
一、静态心形图绘制
(1)效果展示
(2)静态心形原始代码
1 clc; 2 clear all; 3 const=0; 4 % 均布三位坐标 5 x=-5:0.05:5; 6 y=-5:0.05:5; 7 z=-5:0.05:5; 8 [x,y,z]=meshgrid(x,y,z); % 绘制三位坐标点 9 % 心形函数 10 f=(x.^2 + (9/4)*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9/80)*y.^2.*z.^3-const; 11 p=patch(isosurface(x,y,z,f,0)); % 连接各点,组成封闭的图形 12 set(p, 'FaceColor', 'red', 'EdgeColor', 'none'); 13 daspect([1 1 1]) 14 view(3) 15 camlight; lighting phong
二、动态心形图绘制
(1)效果展示
如上图所示
(2) 动态心形源代码
1 clc; 2 clear; 3 filename='heart'; 4 % 三位坐标均布 5 [x,y,z]=meshgrid(linspace(-3,3)); 6 % 心形函数 7 p=(x.^2+(9/4)*y.^2+z.^2-1).^3-x.^2.*z.^3-(9/80)*y.^2.*z.^3; 8 [faces,verts,colors] = isosurface(x,y,z,p,0,x); 9 % 循环绘制心形形成的过程 10 for i=1:9 11 figure(i) 12 pp=patch('Faces',faces(1:284+i*1000,:),'Vertices',verts); 13 set(pp,'FaceColor','red','EdgeColor','none'); 14 view(-30,24) 15 axis off 16 axis equal 17 axis tight 18 camlight 19 lighting gouraud 20 pause(0.5) 21 f(i) = getframe(i); 22 imind = frame2im(f(i)); 23 [imind,cm] = rgb2ind(imind,256); 24 if i == 1 25 26 imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',0.5); 27 else 28 imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.5); 29 end 30 close(i) 31 end
人必有所执,方能有所成。