dla生长模型模拟

依旧是采用python和Qt写的,依赖的库有Vpython和numpy,分点生长和线生长两种。151*151的点阵,4000个点,1/3用红色点,1/3用黄色点,1/3用绿色点。

先上两个图,第一个是点生长,第二个是线生长的。point

line

#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore
from visual import *
import numpy as np

class Window( QtGui.QWidget ):
def __init__( self ):
super( Window, self ).__init__()
self.setWindowTitle( "DLA" )
self.resize( 250, 250 )
gridlayout = QtGui.QGridLayout()

self.lines = QtGui.QPushButton( u"线生长" )
gridlayout.addWidget( self.lines, 0, 0, 1, 3 )
self.points = QtGui.QPushButton( "点生长" )
gridlayout.addWidget( self.points, 1, 0, 1, 3 )
self.quit = QtGui.QPushButton("退出")
gridlayout.addWidget( self.quit, 2, 0, 1, 3 )
self.setLayout( gridlayout )
self.connect(self.lines, QtCore.SIGNAL("clicked()"),self.Lines)
self.connect(self.points, QtCore.SIGNAL("clicked()"),self.Points)
self.connect(self.quit, QtCore.SIGNAL("clicked()"),self.Close)

def Lines(self):
a=np.zeros([n,n])
a[n/2,]=1
plot(a)
return
def Points(self):
a=np.zeros([n,n])
a[n/2,n/2]=1
plot(a)
return
def Close(self):
self.close()

def plot(a):
p=0
while True:
x=int(np.random.random()*(n-1)+1)
y=int(np.random.random()*(n-1)+1)
while True:
k=int(4*np.random.random())+1
if x<0 or x>n-1 or y<0 or y>n-1:
break
if (x-1)<0 or (x+1)>n-1 or (y-1)<0 or (y+1)>n-1:
break

if k==1:
if a[x+1,y]!=1:
x=x+1
else:
a[x,y]=1
if x>n:
break

if k==2:
if a[x,y+1]!=1:
y=y+1
else:
a[x,y]=1
if y>n:
break

if k==3:
if a[x-1,y]!=1:
x=x-1
else:
a[x,y]=1
if x<0: break if k==4: if a[x,y-1]!=1: y=y-1 else: a[x,y]=1 if y<0: break if a[x,y]==1: p=p+1 if p2*numbers/3:
sphere(pos=(x-n/2,y-n/2,0),radius=0.5,color=color.green)
else:
sphere(pos=(x-n/2,y-n/2,0),radius=0.5,color=color.orange)
break
if p>numbers:
break
return

n=151
numbers=4000
a=np.zeros([n,n])

app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

4条评论

  1. Mozilla 1.9.0.11 Mozilla 1.9.0.11 Windows XP Windows XP
    Mozilla/4.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.0.11)

    完全看不懂啊?我是学生物信息的

    1. Firefox 31.0 Firefox 31.0 Windows 8.1 x64 Edition Windows 8.1 x64 Edition
      Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0

      简单的撒点而已

  2. Maxthon 4.4.1.2000 Maxthon 4.4.1.2000 Windows XP Windows XP
    Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.1.2000 Chrome/30.0.1599.101 Safari/537.36

    留言是种美德,写点什么…

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注