利用Matlab复刻两款粒子爱心动画效果

粒子爱心1

function particleHeart1
% @author : slandarer

% 调整背景及比例
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

效果图

利用Matlab复刻两款粒子爱心动画效果_第1张图片

粒子爱心2

function particleHeart2
% @author : slandarer

% 所需匿名函数
col1Func=@(n) repmat([255,158,196]./255,[n,1])+repmat([-39,-81,-56]./255,[n,1]).*repmat(rand([n,1]),[1,3]);
col2Func=@(n) repmat([118,156,216]./255,[n,1])+repmat([137,99,39].*.1./255,[n,1]).*repmat(rand([n,1]),[1,3]);
aFunc=@(n) eval(char([100,105,115,112,40,39,20316,32773,58,115,108,97,110,100,97,114,101,114,39,41]));
szFunc=@(n) rand([n,1]).*15+8;

hold on
% 计算爱心点位置并绘制爱心
n=120;
x=linspace(-3,3,n); 
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);
FV=isosurface(F,0);
hpnts=FV.vertices;
hpnts=(hpnts-repmat(mean(hpnts),[size(hpnts,1),1])).*repmat([.75,.7,.7],[size(hpnts,1),1]);
hpnts=hpnts+rand(size(hpnts)).*.7;
heartHdl=scatter3(hpnts(:,1),hpnts(:,2),hpnts(:,3),'.','SizeData',5,'CData',col1Func(size(hpnts,1)));

% 计算星星位置并绘制星星
sx1=rand([2e3,1]).*120-60;
sy1=rand([2e3,1]).*120-60;
sz1=ones(size(sx1)).*-30;
star1Hdl=scatter3(sx1,sy1,sz1,'.','SizeData',szFunc(length(sx1)),'CData',col2Func(size(sx1,1)),'LineWidth',1);
sx2=rand([2e3,1]).*120-60;
sy2=rand([2e3,1]).*120-60;
sz2=rand([2e3,1]).*120-20;
star2Hdl=scatter3(sx2,sy2,sz2,'.','SizeData',szFunc(length(sx2)),'CData',[1,1,1]);

% 坐标区域修饰
ax=gca;
ax.XLim=[-30,30];
ax.YLim=[-30,30];
ax.ZLim=[-40,30];
ax.Projection='perspective';
% ax.DataAspectRatio=[1,1,1];
view(-42,14);aFunc(1);
ax.Color=[0,0,0];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';
set(ax,'LooseInset',[0,0,0,0]);
set(ax,'Position',[-1/5,-1/5,1+2/5,1+2/5])
set(gcf,'Color',[0,0,0]);

% text(0,0,20,'slandarer','Color','w','HorizontalAlignment','center')

% 旋转爱心和星星
theta1=0;theta2=0;theta3=0;
while 1
    theta1=theta1-0.01;
    theta2=theta2-0.003;
    theta3=theta3-0.02;
    set(heartHdl,'XData',hpnts(:,1).*cos(theta1)-hpnts(:,2).*sin(theta1),...
                 'YData',hpnts(:,1).*sin(theta1)+hpnts(:,2).*cos(theta1))
    set(star1Hdl,'XData',sx1.*cos(theta2)-sy1.*sin(theta2),...
                 'YData',sx1.*sin(theta2)+sy1.*cos(theta2))
    set(star2Hdl,'XData',sx2.*cos(theta3)-sy2.*sin(theta3),...
                 'YData',sx2.*sin(theta3)+sy2.*cos(theta3))
    pause(0.05)
end
end

效果图

利用Matlab复刻两款粒子爱心动画效果_第2张图片

到此这篇关于利用Matlab复刻两款粒子爱心动画效果的文章就介绍到这了,更多相关Matlab粒子爱心动画内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(利用Matlab复刻两款粒子爱心动画效果)