理科生的浪漫:matlab 函数实现心形绘制(2D)

clc,clear,close all;
x=-2:0.001:2;
for a=1:0.1:30
p=3.1415926;
y = abs(power(x,(2/3))) + (0.99 * power((3.3 - power(x,2)),(1/2))).*sin(a*p*x);
h = plot(x,y,'*');
xlim([-3,3]);
ylim([-4,4]);
set(h,'XData',x,'YData',y);
set(h,'Color',[1,0,0]);
set(h,'Marker','.');
set(h,'MarkerSize', 0.02);
refreshdata(h,'caller');
drawnow ;
end
% grid on;
(3D)

跳动的心
clear; clc; close all;
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;
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);
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,-2.0,2.0]);
view(3)
% title()
warning('off');
T = suptitle('I Love You');
set(T,'Interpreter','latex','FontSize',24)
pic_num = 1;
for m=1:18
for i = 1:35
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:35
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
end
这篇文章展示了如何使用MATLAB编程语言来创建2D和3D的心形图形。在2D部分,通过循环改变参数绘制了一系列点形成动态的心形图案。接着,3D部分展示了跳动的心脏效果,利用isosurface函数构建表面并配合颜色映射,最后将帧合成GIF动画,实现了心脏跳动的视觉效果。
4279

被折叠的 条评论
为什么被折叠?



