代码1:动态爱心
Matlab
function particleHeart1
% 调整背景及比例
ax=gca;hold on
ax.DataAspectRatio=[1,1,1];
ax.XLim=[-25,25];
ax.YLim=[-25,20];
ax.Color=[0,0,0];
ax.XColor='none';
ax.YColor='none';
set(gcf,'Color',[0,0,0]);
% 散点位置计算函数及扩散函数
tFunc=@(n) rand([1,n]).*pi*(2-12e-2)+pi.*6e-2+pi;
dFunc=@(t) t(t>2*pi+2e-1|t<2*pi-2e-1);
xFunc=@(t) 16.*sin(t).^3;
yFunc=@(t) 13.*cos(t)-5.*cos(2.*t)-2.*cos(3.*t)-cos(4.*t);
sFunc=@(x,y,b) deal(b.*log(rand(size(x))).*x+x, b.*log(rand(size(y))).*y+y);
rFunc1=@(x,y,r) deal(r./1.2./(sqrt(x.^2+y.^2)+1).^1.8.*x+x+rand(size(x))./10,...
r./1.2./(sqrt(x.^2+y.^2)+1).^1.8.*y+y+rand(size(y))./10);
rFunc2=@(x,y,r) deal(r./1.2./(sqrt(x.^2+y.^2)+1).^1.8.*x+x,r./1.2./(sqrt(x.^2+y.^2)+1).^1.8.*y+y);
aFunc=@(n) eval(char([100,105,115,112,40,39,20316,32773,58,115,108,97,110,100,97,114,101,114,39,41]));
cFunc=@(n) repmat([255,158,196]./255,[n,1])+repmat([-39,-81,-56]./255,[n,1]).*repmat(rand([n,1]),[1,3]);
% 生成随机点
t1=dFunc(tFunc(4e3));L1=length(t1);
t2=dFunc(tFunc(2e3));L2=length([t1,t2]);
t3=dFunc(tFunc(2e3));aFunc(1);
[x1,y1]=sFunc(xFunc(t1),yFunc(t1),.05);
[x2,y2]=sFunc(xFunc(t2),yFunc(t2),.15);
[x3,y3]=sFunc(xFunc(t3).*1.4,yFunc(t3).*1.4,.18);
x0=[x1,x2,x3];y0=[y1,y2,y3];
% 循环绘图
pHdl=scatter(x0,y0,'.','CData',cFunc(length(x0)),'SizeData',8);
for i=1:1e10
[x1,y1]=rFunc2(x0(1:L1),y0(1:L1),10*sin(i/10*pi));
[x2,y2]=rFunc1(x0(L1+1:L2),y0(L1+1:L2),10*sin(i/10*pi));
[x3,y3]=rFunc1(x0(L2+1:end),y0(L2+1:end),10*sin((i+10)/10*pi));
x=[x1,x2,x3];y=[y1,y2,y3];
pHdl.XData=x;
pHdl.YData=y;
drawnow;
pause(.05)
end
end
这段代码通过粒子动画的形式绘制了一个动态的爱心,其中包含了爱心的生成和动态变化的效果。
代码2:动态爱心
Matlab
clear; clc;
n = 100;
t = linspace(0, 2*pi, n);
% 心形图极坐标表示法
x = 16*sin(t).^3;
y = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
handle = scatter(x, y, '.m');
% 调整背景和范围
ax = gca;
ax.Color = [0, 0, 0];
ax.XColor = 'none';
ax.YColor = 'none';
hold on;
handle0 = scatter(x, y, '.m');
hold off;
% 径向有规律缩放
len = 2000;
tt = linspace(0, 2*pi, len);
for i = 1:len
% 背景随机一组大点的心形
r0 = 10 - abs(normrnd(10, 2, [1, len]) - 10);
x0 = r0 * 16.*sin(tt).^3;
y0 = r0 .* (13*cos(tt) - 5*cos(2*tt) - 2*cos(3*tt) - cos(4*tt));
handle0.XData = 1.2*x0;
handle0.YData = 1.2*y0;
% 根据距离缩放
d = sqrt(x.^2 + y.^2);
l = (d - min(d)) / (max(d) - min(d));
c = 1 + (2 - 1.8*l)/8.*sin(80*tt(i));
handle.XData = c.*x;
handle.YData = c.*y;
drawnow;
pause(0.01);
end
这段代码创建了一个跳动的3D心形图,通过有规律的缩放和抖动来模拟心跳的效果。