依旧是采用python和Qt写的,依赖的库有Vpython和numpy,分点生长和线生长两种。151*151的点阵,4000个点,1/3用红色点,1/3用黄色点,1/3用绿色点。
#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 p
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_()
很有趣呀
完全看不懂啊?我是学生物信息的
简单的撒点而已
留言是种美德,写点什么…