jjj1 发表于 2020-7-29 10:26:51

表白代码

动态心形线(来自微信公众号 MATLAB与数学建模)
clear
close all
%确定x,y,z坐标取值
=meshgrid(linspace(-3,3,101));
axis off;
set(gcf,'color',);
%定义三维心形线函数,求解f(x,y,z)=0,可得心形线坐标
f=@(x,y,z)(x.^2+ (9/4)*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9/80)*y.^2.*z.^3;
val=f(x,y,z);
view(3);
%建立gif文件
= rgb2ind(frame2im(getframe),256);
imwrite(A,map,'xin.gif','LoopCount',65535,'DelayTime',0.1);
%绘制与YOZ平行的平面上的心形线
for X = 35:67
    %提取不同X取值下,与YOZ平行的平面上,f(y,z)取值
    plane = reshape(val(:,X,:),101,101);
    %提取val为0的等值线,即满足f(y,z)=0的坐标点
    c_data = contourc(plane,);
    x_data = X*ones(1,c_data(2,1));
    %绘制在YOZ平行平面上的心形线
    plot3(x_data,c_data(2,2:end),c_data(1,2:end),'r');
    axis()
    axis off
    hold on
    %将当前图像保存到gif文件中
    = rgb2ind(frame2im(getframe),256);
    imwrite(A,map,'xin.gif','WriteMode','append','DelayTime',0.2);
end
%绘制XOZ平行平面上的心形线
for Y = 41:61
    plane = reshape(val(Y,:,:),101,101);
    c_data = contourc(plane,);
    y_data = Y*ones(1,c_data(2,1));
    plot3(c_data(2,2:end),y_data,c_data(1,2:end),'r');
    axis()
    = rgb2ind(frame2im(getframe),256);
    imwrite(A,map,'xin.gif','WriteMode','append','DelayTime',0.2);
end
%绘制XOY平行面上的心形线
for Z = 36:71
    plane = val(:,:,Z);
    c_data = contourc(plane,);
    s = 1;
    if length(c_data(2,:)) > (c_data(2,1)+1)
      s = c_data(2,1)+2;
      z_data = Z*ones(1,c_data(2,1));
      hold on
      plot3(c_data(1,2:(s-1)),c_data(2,2:(s-1)),z_data,'r');
      axis()
       = rgb2ind(frame2im(getframe),256);
      imwrite(A,map,'xin.gif','WriteMode','append','DelayTime',0.2);
      pause('on')
      pause(0.2)
    end
    z_data = Z.*ones(1,c_data(2,s));
    plot3(c_data(1,(s+1):end),c_data(2,(s+1):end),z_data,'r');
    axis()
    = rgb2ind(frame2im(getframe),256);
    imwrite(A,map,'xin.gif','WriteMode','append','DelayTime',0.2);
end

johnny_i 发表于 2021-6-6 15:15:01

这个很酷!!!

johnny_i 发表于 2021-6-6 15:15:52

这个也太酷了吧!!!
页: [1]
查看完整版本: 表白代码