matlab也可以画出浪漫爱心

举报
川川菜鸟 发表于 2021/10/15 23:21:27 2021/10/15
【摘要】 (会生成gif文件)代码: %跳动爱心 clear; clc; close all; % NOTICE: Your MATLAB version should be at least R2019a...

在这里插入图片描述

(会生成gif文件)代码:

%跳动爱心
clear; clc; close all; 
% NOTICE: Your MATLAB version should be at least R2019a !

% --------------------------- functions -------------------------------
f = @(x, y, z)(x.^2 + 2.25*y.^2 + z.^2 - 1).^3 -  ...
    x.^2.* z.^3 - 0.1125*y.^2.*z.^3;
g = @(x, y, z)(sqrt(x.^2+y.^2)-2.5).^2 + z.^2 - 0.4^2;

% -------------------------- generate data ----------------------------
t = linspace(-5, 5);

[x1, y1, z1] = meshgrid(t);
[x2, y2, z2] = meshgrid(t);

val1 = f(x1, y1, z1);
val2 = g(x2, y2, z2);

[p1, v1] = isosurface(x1, y1, z1, val1, 0);
[p2, v2] = isosurface(x2, y2, z2, val2, 0);

% --------------------------- basic plot ------------------------------
figure()
subplot(1, 1, 1)

h = patch('faces',p1,'vertices',v1,'facevertexcdata',jet(size(v1,1)),...
    'facecolor','w','edgecolor','flat'); hold on;
patch('faces',p2,'vertices',v2,'facevertexcdata',jet(size(v2,1)),...
    'facecolor','w','edgecolor','flat');
grid on; axis equal; axis([-3,3,-3,3,-1.5,1.5]); view(3)
title(["$(x^2+\frac{9}{4}y^2+z^2-1)^3-x^2z^3-\frac{9}{80}y^2z^3=0$",...
    "$(\sqrt{x^2+y^2}-R)^2 +z^2 = r^2$"],'Interpreter','latex','position',[3.3,4])
warning('off');
% 请在此处进行您的演讲!% 请在此处进行您的演讲!
T = suptitle("$I\ Love\ U\ !$"); 
% 请在此处进行您的演讲!% 请在此处进行您的演讲!
set(T,'Interpreter','latex','FontSize',24)

% -------------------------- generate gif -----------------------------
pic_num = 1;
for i = 1:20
    v1 = 0.98 * v1;
    set(h, 'vertices', v1); drawnow;
    F = getframe(gcf);
    I = frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num == 1
        imwrite(I,map,'BeatingHeart.gif','gif','Loopcount',inf,'DelayTime',0.05);
    else
        imwrite(I,map,'BeatingHeart.gif','gif','WriteMode','append','DelayTime',0.05);
    end
    pic_num = pic_num + 1;
end
for i = 1:20
    v1 = v1 / 0.98;
    set(h, 'vertices', v1); drawnow;
    F = getframe(gcf);
    I = frame2im(F);
    [I,map] = rgb2ind(I,256);
    imwrite(I,map,'BeatingHeart.gif','gif','WriteMode','append','DelayTime',0.05);
    pic_num = pic_num + 1;
end

% ----------------------------- dynamic -------------------------------
while true
    for i = 1:20
        v1 = 0.98 * v1;
        set(h, 'vertices', v1); drawnow;
    end
    for i = 1:20
        v1 = v1 / 0.98;
        set(h, 'vertices', v1); drawnow;
    end
end
% ------------------------------- end ---------------------------------

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

在这里插入图片描述
代码:

%爱心1
clear all
clc
f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
[x,y,z]=meshgrid(linspace(-3,3));
val=f(x,y,z);
[p,v]=isosurface(x,y,z,val,0);
patch('faces',p,'vertices',v,'facevertexcdata',jet(size(v,1)),'facecolor','w','edgecolor','flat');
view(3);grid on;axis equal;

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

%爱心2
clear all
clc
f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
[x,y,z]=meshgrid(linspace(-1.5,1.5));
val=f(x,y,z);
isosurface(x,y,z,val,0); 
axis equal;view(3);colormap([1 0.2 0.2])

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述
该爱心公式:
在这里插入图片描述

这个你则需要建立一个函数文件HeartRate.m如下

function ratio=HeartRate(i,heartSpeed)
i=mod(i,heartSpeed);
if i < heartSpeed / 2
    ratio=sin( i/heartSpeed*pi);
elseif i >= heartSpeed / 2 && i < heartSpeed * 3/4
    ratio=2*cos(i/heartSpeed*2/3*pi);
else
    ratio = 0;
end

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

再到命令行窗口执行:

%爱心3
gcp=figure;
M=moviein(100,gcp);
heartSpeed=15;
radiusRate=3;

for i = 1:100
    
    axis([-20 20 -20 20]);
    cla(gcp);
    x=[];
    y=[];
    
    for t = 0:0.01:2*pi
        r=(sin(t)*sqrt(abs(cos(t))))/(sin(t)+7/5)-2*sin(t)+2;
        r = radiusRate * r;
        x=[x,r*cos(t)*(0.5+0.5*HeartRate(i,heartSpeed))];
        y=[y,r*sin(t)*(0.5+0.5*HeartRate(i,heartSpeed))];
    end
    hold on;
    fill(x,y,'r');
    
    M(:,i)=getframe;
end
movie(M,1);

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

本篇为个人收集后测试的效果。

文章来源: chuanchuan.blog.csdn.net,作者:川川菜鸟,版权归原作者所有,如需转载,请联系作者。

原文链接:chuanchuan.blog.csdn.net/article/details/119803093

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。