Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 3.51 KB | Hits: 78 | Expires: Never
Copy text to clipboard
  1. from graphics import *
  2. import math
  3. from math import *
  4. import random
  5. from random import *
  6.  
  7. def buildWindow():
  8.     win=GraphWin("Random Race", 500, 600)
  9.     xAxis=Line(Point(250,0),Point(250,500)) #axes
  10.     xAxis.setWidth(2)
  11.     xAxis.draw(win)
  12.     yAxis=Line(Point(0,250),Point(500,250))
  13.     yAxis.setWidth(2)
  14.     yAxis.draw(win)
  15.    
  16.     Line(Point(150,0),Point(150,500)).draw(win) #other lines
  17.     Line(Point(50,0),Point(50,500)).draw(win)
  18.     Line(Point(350,0),Point(350,500)).draw(win)
  19.     Line(Point(450,0),Point(450,500)).draw(win)
  20.     Line(Point(0,150),Point(500,150)).draw(win)
  21.     Line(Point(0,50),Point(500,50)).draw(win)
  22.     Line(Point(0,350),Point(500,350)).draw(win)
  23.     Line(Point(0,450),Point(500,450)).draw(win)
  24.  
  25.     rectangle=Rectangle(Point(5,500),Point(495,595)) #information box
  26.     rectangle.setFill("Yellow")
  27.     rectangle.draw(win)
  28.  
  29.     Text(Point(220,515),"Step #").draw(win) #box labels
  30.     Text(Point(80,535),"Red distance = ").draw(win)
  31.     Text(Point(320,535),"Red maximum distance = ").draw(win)
  32.     Text(Point(80,555),"Green distance = ").draw(win)
  33.     Text(Point(320,555),"Green maximum distance = ").draw(win)
  34.    
  35.     return win
  36.  
  37. def generateData():
  38.     change=uniform(0,2*math.pi) #get the random change
  39.     y=5*math.sin(change) #get the y change
  40.     x=5*math.cos(change) #get the x change
  41.     return x, y
  42.  
  43. def distToOrigin(p): #calculate distance to origin from particular point
  44.     dx=p.x-250
  45.     dy=p.y-250
  46.     return sqrt((dx*dx)+(dy*dy))
  47.  
  48. def main():
  49.     win=buildWindow()
  50.    
  51.     red=Point(250,250) #create starting points
  52.     green=Point(250,250)
  53.     redMaxDistance=0 #track max distances
  54.     greenMaxDistance=0
  55.    
  56.     stepLabel=Text(Point(260,515),"0") #great label text
  57.     redDistLabel=Text(Point(170,535),"0")
  58.     redMaxLabel=Text(Point(450,535),"0")
  59.     greenDistLabel=Text(Point(170,555),"0")
  60.     greenMaxLabel=Text(Point(450,555),"0")
  61.    
  62.     for i in range(0,501):
  63.         xChRe,yChRe=generateData() #get random changes
  64.         newRed=Point(red.x+xChRe,red.y+yChRe) #get new point
  65.         redLine=Line(red,newRed) #link old point and new point
  66.         redLine.setFill("Red")
  67.         redLine.draw(win)
  68.         red=newRed #set new point as old point for next run
  69.        
  70.         xChGr,yChGr=generateData() #same as for red
  71.         newGreen=Point(green.x+xChGr,green.y+yChGr)
  72.         greenLine=Line(green,newGreen)
  73.         greenLine.setFill("Green")
  74.         greenLine.draw(win)
  75.         green=newGreen
  76.        
  77.         stepLabel.undraw() #update step number
  78.         stepLabel.setText(str(i))
  79.         stepLabel.draw(win)
  80.  
  81.         redDist=round(distToOrigin(red),3) #get distances
  82.         greenDist=round(distToOrigin(green),3)
  83.  
  84.         redDistLabel.undraw() #update distances
  85.         redDistLabel.setText(str(redDist))
  86.         redDistLabel.draw(win)
  87.  
  88.         greenDistLabel.undraw()
  89.         greenDistLabel.setText(str(greenDist))
  90.         greenDistLabel.draw(win)
  91.  
  92.         if(redDist>redMaxDistance): #update max distances if applicable
  93.             redMaxDistance=redDist
  94.             redMaxLabel.undraw()
  95.             redMaxLabel.setText(str(redDist))
  96.             redMaxLabel.draw(win)
  97.  
  98.         if(greenDist>greenMaxDistance):
  99.             greenMaxDistance=greenDist
  100.             greenMaxLabel.undraw()
  101.             greenMaxLabel.setText(str(greenDist))
  102.             greenMaxLabel.draw(win)
  103.  
  104.        
  105.     Text(Point(250,580),"Click to close window").draw(win)        
  106.     win.getMouse()
  107.     win.close()
  108.    
  109. if __name__ == '__main__':
  110.     main()