Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.su/hg/petri_dish/diff/2df48c61bd42/scale.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 06:02:04 2014
Кодировка:
petri_dish: scale.py diff

petri_dish

diff scale.py @ 20:2df48c61bd42

now cells can move,reproduce
author Yashina Ksenia <ksenia_yashina@kodomo.fbb.msu.ru>
date Mon, 20 Dec 2010 01:44:18 +0300
parents 9fa0119fa63c
children 69a4724e7cac
line diff
     1.1 --- a/scale.py	Mon Dec 20 01:42:34 2010 +0300
     1.2 +++ b/scale.py	Mon Dec 20 01:44:18 2010 +0300
     1.3 @@ -1,9 +1,12 @@
     1.4  from Tkinter import *
     1.5 -import Bacteria
     1.6 -import Petri
     1.7 +from math import *
     1.8 +import bacteria
     1.9 +import petri
    1.10 +import random
    1.11 +import vector
    1.12  
    1.13  class IF(object):
    1.14 -    def __init__(self,delay=10,diam=300,num=1,env=1,reprod_a=False,reprod_s=False,life=1):
    1.15 +    def __init__(self,delay=2,diam=300,num=1,env=1,reprod_a=False,reprod_s=False,life=1):
    1.16          self.delay=delay
    1.17          self.diam=diam
    1.18          self.num=num
    1.19 @@ -16,10 +19,14 @@
    1.20          return "<%s,%s,%s,%s,%s,%s,%s>"%(self.delay,self.diam,self.num,\
    1.21                                           self.env,self.reprod_a,\
    1.22                                           self.reprod_s,self.life)
    1.23 -    def mutate():
    1.24 -        pass
    1.25 +    def mutate(self):
    1.26 +        for bact in p.bacteria:
    1.27 +            if not(bact.mutated):
    1.28 +                bact.mutated=True
    1.29 +                return
    1.30  
    1.31      def sbmt(self):
    1.32 +        b=[]
    1.33          submit.config(text="Stop",command=self.stop)
    1.34          scl1.config(state=DISABLED)
    1.35          scl2.config(state=DISABLED)
    1.36 @@ -33,9 +40,25 @@
    1.37          filler=c.create_rectangle(0,0,self.diam+22,self.diam+22,fill="lightgrey")
    1.38          c.grid(row=0,column=2,rowspan=6,padx=50,pady=10)
    1.39          dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")
    1.40 -        print self
    1.41 +        for i in range(self.num):
    1.42 +            x_i=random.randint(-self.diam/2+15,self.diam/2-15)
    1.43 +            y_i=random.randint(15+(int)(-sqrt((self.diam/2)**2-x_i**2)),-15+(int)(sqrt((self.diam/2)**2-x_i**2)))
    1.44 +            b.append(bacteria.Bacteria(vector.Vector(x_i,y_i),
    1.45 +                              vector.Vector(0,1),
    1.46 +                              self.life,False))
    1.47 +        for bact in b:
    1.48 +            bact.velocity = bact.rnd_velocity(pi/4*random.randrange(0,8))
    1.49 +        p.num=self.num
    1.50 +        p.bacteria=b
    1.51 +        p.radius=self.diam/2
    1.52 +        p.delta=5
    1.53 +        p.bact_r=5
    1.54 +        p.full_lifetime=self.life
    1.55 +        self.depict()
    1.56  
    1.57      def stop(self):
    1.58 +        c.after_cancel(self.rep)
    1.59 +        c.delete("all")
    1.60          submit.config(text="Submit",command=self.sbmt)
    1.61          scl1.config(state=NORMAL)
    1.62          scl2.config(state=NORMAL)
    1.63 @@ -58,18 +81,32 @@
    1.64          pass
    1.65      
    1.66      def depict(self):
    1.67 -        pass
    1.68 +        c.delete("all")
    1.69 +        dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")
    1.70 +        env=scl.get()
    1.71 +        p.step(0.5,int(env),self.reprod_a,self.reprod_s)
    1.72 +        for bact in p.bacteria:
    1.73 +            if bact.mutated:
    1.74 +                color="black"
    1.75 +            else:
    1.76 +                color="white"
    1.77 +            c.create_oval(10+self.diam/2+bact.position.x-p.bact_r,10+self.diam/2+bact.position.y-p.bact_r,\
    1.78 +                          10+self.diam/2+bact.position.x+p.bact_r*2,\
    1.79 +                          10+self.diam/2+bact.position.y+p.bact_r*2,fill=color)
    1.80 +        self.rep=c.after(self.delay,self.depict)
    1.81  
    1.82 -def update_scale(widget, times):
    1.83 -    value = widget.get()
    1.84 -    length = widget['to'] - widget['from']
    1.85 -    value += length * times / 10
    1.86 -    widget.set(value)
    1.87 +    def update_scale(widget, times):
    1.88 +        value = widget.get()
    1.89 +        length = widget['to'] - widget['from']
    1.90 +        value += length * times / 10
    1.91 +        widget.set(value)
    1.92  
    1.93  root = Tk()
    1.94  root.title("Interface")
    1.95  intf=IF()
    1.96  c=Canvas()
    1.97 +p=petri.Petri()
    1.98 +b=[]
    1.99  
   1.100  root.bind_class("Scale", "<4>", lambda ev: update_scale(ev.widget, +1))
   1.101  root.bind_class("Scale", "<5>", lambda ev: update_scale(ev.widget, -1))
   1.102 @@ -98,7 +135,7 @@
   1.103  init_frame=LabelFrame(root,text="Initial parameters")
   1.104  init_frame.grid(padx=15,pady=10)
   1.105  scl1 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=1,\
   1.106 -                to=50,sliderlength=7,width=10)
   1.107 +                to=10,sliderlength=7,width=10)
   1.108  scl1.grid(row=0, column=0,padx=10,pady=5)
   1.109  lbl1 = Label(init_frame,text="number of cells",bg="white")
   1.110  lbl1.grid(row=0,column=1,padx=10)
   1.111 @@ -108,7 +145,7 @@
   1.112  lbl2 = Label(init_frame,text="diameter of Petri dish",bg="white")
   1.113  lbl2.grid(row=1,column=1,padx=10)
   1.114  scl3 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=1,\
   1.115 -                to=30,sliderlength=7,width=10)
   1.116 +                to=1000,sliderlength=7,width=10)
   1.117  scl3.grid(row=2, column=0,padx=10,pady=5)
   1.118  lbl3 = Label(init_frame,text="cells' lifetime",bg="white")
   1.119  lbl3.grid(row=2,column=1,padx=10)