【matlab】感谢广大网友的智慧,让我学习、收集、罗列了7种画爱心的方案

由简到难

1、

ezplot('(x^2-1+y^2)^3-x^2*y^3 = 0')  %隐函数作图,想比于plot不用指定变量范围,很适合用来调曲线哦~ 自己发挥吧
axis([-2.5 2.5 -2 2])  %范围

在这里插入图片描述

2

n=100;
x=linspace(-3,3,n); %nirv
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);  %心型函数
isosurface(F,0)
lighting phong
caxis  
axis equal
colormap('flag'); %颜色
view([60 30]); %视角

在这里插入图片描述

3,动态心,输出gif

[x,y,z]=meshgrid(linspace(-3,3)); 
p=(x.^2+(9/4)*y.^2+z.^2-1).^3-x.^2.*z.^3-(9/80)*y.^2.*z.^3; 
[faces,verts,colors] = isosurface(x,y,z,p,0,x);
for i=1:9
figure(i)
pp=patch('Faces',faces(1:284+i*1000,:),'Vertices',verts);
set(pp,'FaceColor','red','EdgeColor','none');
view(-30,24)
axis off
axis equal
axis tight
camlight
lighting gouraud
pause(0.5)
f(i) = getframe(i);
imind = frame2im(f(i));
[imind,cm] = rgb2ind(imind,256);
if i == 1
        imwrite(imind,cm,'heart.gif', 'Loopcount',inf,'DelayTime',0.5);
else
        imwrite(imind,cm,'heart.gif','WriteMode','append','DelayTime',0.5);
end
close(i)
end

在这里插入图片描述

4

%% 4爱心的参数方程使用plot
t=0:0.1:2*pi;
x=16*sin(t).^3;
y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t);
%%动图画图并保存为GIF
figure;
h = plot(x(1),y(1),'r');
axis([-20,20,-20,20]);
axis off
[A,map] = rgb2ind(frame2im(getframe),256);%getframe获取当前画面
imwrite(A,map,'1.gif','LoopCount',inf,'DelayTime',0.1);
for idx = 2:length(t)
    h.XData(idx) = x(idx);
    h.YData(idx) = y(idx);
    drawnow
    [A,map] = rgb2ind(frame2im(getframe),256);
    imwrite(A,map,'1.gif','WriteMode','append','DelayTime',0.1);  
end

在这里插入图片描述

5

%% 5爱心方程使用animatedline+addpoints
t=0:0.1:2*pi;
x=16*sin(t).^3;
y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t);
%%动图画图并保存为GIF
h = animatedline('Color','r');%创建动画线型
axis([-20,20,-20,20]);
axis off
[A,map] = rgb2ind(frame2im(getframe),256);
imwrite(A,map,'2.gif','LoopCount',inf,'DelayTime',0.1);
for idx = 1:length(t)
    addpoints(h,x(idx),y(idx));
    drawnow
    [A,map] = rgb2ind(frame2im(getframe),256);
    imwrite(A,map,'2.gif','WriteMode','append','DelayTime',0.1);
end

效果同4

6用正余弦画心形

for b=0:0.08:20 
    clf;
    x=[0:0.01:1.8];
    y=x.^(2/3)+0.9*sqrt(3.3-x.^2).*sin(b.*pi.*x);
    plot(x,y,'r');
    hold on
    plot(-x,y,'r')
    plot(x*2/3,y*2/3,'b')
    plot(-x*2/3,y*2/3,'b')
    axis equal
    pause(0.0005)
    hold off
    [A,map] = rgb2ind(frame2im(getframe),256);
    if b == 0
        imwrite(A,map,'3.gif', 'Loopcount',inf,'DelayTime',0.5);
    else
        imwrite(A,map,'3.gif','WriteMode','append','DelayTime',0.5);
    end
end

在这里插入图片描述

7旋转心形动画代码

%在MATLAB中运行
%参考https://download.csdn.net/download/windywoody/5485279
%
clear all;
close all;
clc;
%配置函数,建立图形
f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3.*2 - (9./80).*y.^2.*z.^3;%心形的方程
[x,y,z]=meshgrid(-2:0.04:2); %设置网格密度,中间的数字越小,格越密,形状越逼真,但是太密可能导致内存不足而无法运行
 val=f(x,y,z);
 [p,v]=isosurface(x,y,z,val,0);
 %配置显示环境
 figure('Color',[0 0 0],'Position',[200,100,800,500]);
 axes('Color',[0 0 0],'XColor',[1 1 1],'YColor',[1 1 1],'ZColor',[1 1 1],'FontSize',1,'Visible','off');
 patch('faces',p,'vertices',v,'facevertexcdata',jet(size(v,1)),'facecolor','none','edgecolor','interp');
 % 'color' 后面可以是 'r','g','b','c','m','y','w', or 'k',对应不同的颜色,或者[x,y,z],x,y,z是MATLAB中对颜色的表述,具体自行查资料吧
 %'facecolor''edgecolor' 可以是 'flat' 'none', or 'interp',亦或者上一条注释
 view(3);grid off;axis equal;
 
 %配置显示文本
 da=[126  21608 23567 2345 1996 23478 126];%标题内容,每个字符都用数值表示,不运行看不出来什么内容,执行 double('字符串') 可以看到“字符串”对应的数值
 db=[32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 126 108 117 118 32 102 114 111 109 32  2168 3092];%落款内容,原理同上
 msg1=text(0,0.3,2,char(da),'Color','r','FontSize',20,'HorizontalAlignment','center');%显示标题
 text(0,0,-1.5,char(db),'Color','r','FontSize',10,'HorizontalAlignment','left');%显示落款
 text(0,0.3,1.75,'\heartsuit \^\_\^ \heartsuit','Color','r','FontSize',20,'HorizontalAlignment','center');

 
h=gca;
axis vis3d

%配置文字闪烁效果/标题交替显示
sl=10;%减速倍数,控制文字闪烁速度
mn=4;%文字闪烁更替出现的颜色数,对应下面的if/elseif次数


%配置滚动字幕
msg2=text(0,0,-1.1,'','Color','y','FontSize',10,'HorizontalAlignment','right');%配置小字号显示属性和位置
msg3=text(0,0,-1.3,'','Color','y','FontSize',14,'HorizontalAlignment','right');%配置大字号显示属性和位置

txtmsg=char([
20146   29233   30340       126  	32	32	32	32	32	32	32	32	32	32
23453	36125	126         32      32  32	32	32	32	32	32	32	32	32
25105	29233	20320       126     126	32	32	32	32	32	32	32	32	32
22909	29233	22909       29233	126	126	126	32	32	32	32	32	32	32
20040	20040	32          126     32	92	94	32	51	32	92	94	32	126


]);%滚动字幕默认内容,注意,每行字符数必须相同,字数不够的末尾补空格,即32

%txtmsg=char(importdata('msg.txt'));%从外部文件msg.txt加载文本部分,msg.txt要和代码在同一目录下。无msg.txt可注释掉本行,不影响程序执行。
%一行文字如果太多,图形窗口显示不下,则文字可能显示不出来,所以一行不要写太多。
[msgLength,~]=size(txtmsg);

msgls=100;%减速倍数,控制滚动字幕速度
   for i=0:inf
        
        %文字闪烁
        j=mod(fix(i/sl),mn);
        if j==0
            set(msg1,'Color','r');            
        elseif j==1
            set(msg1,'Color','g');            
        elseif j==2
            set(msg1,'Color','y');
        elseif j==3
            set(msg1,'Color','r');
        end
        
        %滚动字幕
        msgl=mod(fix(i/msgls),msgLength+2);        
        if msgl>msgLength;
            set(msg2,'String','');
        elseif msgl~=0;
            set(msg2,'String',txtmsg(msgl,:));                
        end
        if msgl<msgLength
            set(msg3,'String',txtmsg(msgl+1,:));
        else
            set(msg3,'String','');
        end    
        
        %自动旋转效果
        view(h,i,10);
        drawnow;
    end 

关于matlab中字符和ASCII码互换
参考https://www.baidu.com/link?url=2AYsOyTqJDyTa_SGExsZd4_TSiFPbgrMEun75rCZWi__TIwILlqlRD6fPtQlB2Ubm2ay832TvTB1ivcmpjNcuK&wd=&eqid=9e5a446f000768a8000000055ea9643a
第二个回答,十分清晰
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值