【蒙特卡洛】DLA分形之雪花的制造.
source link: https://www.guofei.site/2015/12/31/fractal.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【蒙特卡洛】DLA分形之雪花的制造.
2015年12月31日Author: Guofei
文章归类: 趣文,文章编号:
版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2015/12/31/fractal.html
先上两张效果图:
很美吧,这是用粒子随机游走一个一个搭建出来的哟。
其原理是让一个粒子等概率上下左右移动,碰到物体后黏在一起。
简洁的算法,最后形成的图案,大片的雪花
、一缕阳光下的灰尘
,都是这种原理形成的。
感受到大自然的美了吗?(文科女神:“臭屌丝……”)
废话不多说,直接上程序,程序里面的图会动哟。
function p=dla()
a=100;b=100;
A=zeros(a,b);
A(ceil(a/2),ceil(b/2))=1;
n=1000;
for i=1:n
con=0;
[A,t]=my1(A);
while con==0
[t,A]=my2(t,A);
[con,A]=my3(t,A,con);
end
imshow(-A,[])
end
end
生成粒子:
function [A,t]=my1(A)%生成粒子
[a,b]=size(A);
cc=ceil(rand*(a+b)*2);
if cc<=a
t=[cc,1];
elseif cc<=a+b
t=[a,cc-a];
elseif cc<=a+a+b
t=[cc-a-b,b];
else
t=[1,cc-a-a-b];
end
end
随机游走:
function[t,A]=my2(t,A)%随机游走1次con=1代表出界
[a,b]=size(A);
cc=rand;
if cc<0.25
t(1)=max(t(1)-1,1);
elseif cc<0.5
t(2)=min(t(2)+1,b);
elseif cc<0.75
t(1)=min(t(1)+1,a);
else t(2)=max(t(2)-1,1);
end
end
粒子黏着:
function [con,A]=my3(t,A,con)%判定粒子是否黏着,返回黏着后的矩阵con=1代表附着
[a,b]=size(A);
if t(1)+1<=a&t(2)+1<=b&t(1)-1>=1&t(2)-1>=1
if A(t(1)+1,t(2))==1
con=1
end
if A(t(1),t(2)+1)==1
con=1
end
if A(t(1)-1,t(2))==1
con=1
end
if A(t(1),t(2)-1)==1
con=1
end
end
if con==1
A(t(1),t(2))=1;
t(1),t(2)
end
end
您的支持将鼓励我继续创作!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK