一、靜態心形圖繪制
(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