来用matlab绘制爱心吧,给出三种绘图方式
实例1
% Method 1
x = -1:1/400:1;
% 给定一个x的范围,指定步长为1/400
y1 = 0.6 * abs(x) .^ 0.5 + ((1 - x .^ 2) / 2) .^ 0.5;
% 爱心的上半部分
y2 = 0.6 * abs(x) .^ 0.5 - ((1 - x .^ 2) / 2) .^ 0.5;
% 爱心的下半部分
fill([x, flip(x)], [y1, y2], 'r')
% 利用fill()函数画出爱心。第一、二个参数均为向量,对应起来恰好是爱心边上的点。
% "r"指定颜色为 red
axis square
% 将当前坐标系图形设置为方形
title('I love U');
结果
有点像屁股的爱心❤。。。
实例2
% Method 2
x = -2:1/400:2;
y = abs(x .^ (2/3)) + (0.99 * (3.3 - x .^ 2) .^ (1/2)) .* sin(9.9 * pi * x);
plot(x, y, 'r');
title('I love U');
结果
实例3
% Method 3
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(1)
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("我爱你 China");
set(T,'Interpreter','latex','FontSize',24)
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
title('I love U');
结果