import random
import matplotlib.pyplot as plt
class clusters:
def __init__(self,size=100,number=200):
self.l=size
self.n=number
self._fig=[[0]*self.l for i in range(self.l)]
def growth(self):
self._fig[int(self.l/2)][int(self.l/2)]=1
counter=0
while counter<self.n:
tempx=random.randint(0,self.l-1)
tempy=random.randint(0,self.l-1)
if tempx==0:
continue
elif tempx==self.l-1:
continue
elif tempy==0:
continue
elif tempy==self.l-1:
continue
self._fig[tempx][tempy]=1
if self._fig[tempx-1][tempy]==1:
continue
elif self._fig[tempx+1][tempy]==1:
continue
elif self._fig[tempx][tempy-1]==1:
continue
elif self._fig[tempx][tempy+1]==1:
continue
while(1):
self._fig[tempx][tempy]=0
temp=random.random()
if temp<0.25:
tempx-=1
elif 0.25<temp<0.5:
tempy-=1
elif 0.5<temp<0.75:
tempx+=1
elif temp>0.75:
tempy+=1
self._fig[tempx][tempy]=1
if tempx==0:
self._fig[tempx][tempy]=0
break
elif tempx==self.l-1:
self._fig[tempx][tempy]=0
break
elif tempy==0:
self._fig[tempx][tempy]=0
break
elif tempy==self.l-1:
self._fig[tempx][tempy]=0
break
if self._fig[tempx-1][tempy]==1:
counter+=1
break
elif self._fig[tempx+1][tempy]==1:
counter+=1
break
elif self._fig[tempx][tempy-1]==1:
counter+=1
break
elif self._fig[tempx][tempy+1]==1:
counter+=1
break
def show(self):
for i in range(self.l):
for j in range(self.l):
if self._fig[i][j]==1:
plt.plot(i,j,'b.')
plt.xlim(0,self.l)
plt.ylim(0,self.l)
plt.grid(True)
plt.xlabel('x')
plt.ylabel('y')
plt.title('DLA cluster,number of particles=%.f'%self.n)
a=clusters()
a.growth()
a.show()
代码4
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 汉诺塔问题 温馨提示:不要复制粘贴,你需要的练习是:自己动手慢慢敲代码一些有用的链接:CP Pascal Edit...
- 曾经有这么一个说法,程序中存在3种类型的bug:你的bug,我的bug和多线程。这虽然是句调侃,但从某种程度上道出...
- Alamofire.request request 函数签名 request 函数实现 SessionManage...
- 1.问题 你知道delegate 如果是strong 修饰的话,就会引起循环引用。导致内存释放不掉,内存泄漏。你...