Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. #!/usr/local/bin/python3.1
  2.  
  3. #Created by Jared Gray and Travis Singer
  4. #Havill
  5. #CS 111
  6. #27 October 2010
  7.  
  8. #Project 5 - Diffusion-Limited Aggregation
  9.  
  10. import math, cTurtle, random
  11.  
  12. def distance(p, q):
  13.     '''Return the distance between points p and q. The input of this function
  14.       must be two lists/tuples, each containing a coordinate value.'''
  15.    
  16.     x1 = p[0]
  17.     x2 = q[0]
  18.     y1 = p[1]
  19.     y2 = q[1]
  20.    
  21.     return math.sqrt((x2-x1)**2+(y2-y1)**2)   #distance formula
  22.  
  23. def findCoordinate(r, angle, origin):
  24.     '''Determines the location of a coordinate r units away from
  25.       the origin at angle'''
  26.  
  27.     x = origin[0]+(math.cos(math.radians(angle))*r)
  28.     y = origin[1]+(math.sin(math.radians(angle))*r)
  29.    
  30.     return [int(round(x, 1)), int(round(y, 1))]    #rounds and makes the coordinate an integer, thus usable in the matrix (introduces error)
  31.    
  32. def adjacent(grid, x, y):
  33.     '''Returns the value True if the particle is adjacent to a particle in the cluster
  34.       and False otherwise.'''
  35.    
  36.     if abs(x) >= len(grid)-1 or abs(y) >= len(grid)-1:   #used to circumvent errors casued by large indeces
  37.         return False
  38.    
  39.     if grid[x+1][y] == 1:
  40.         return True
  41.     elif grid[x][y+1] == 1:
  42.         return True
  43.     elif grid[x+1][y+1] == 1:
  44.         return True
  45.     elif grid[x-1][y] == 1:
  46.         return True
  47.     elif grid[x][y-1] == 1:
  48.         return True
  49.     elif grid[x-1][y-1] == 1:
  50.         return True
  51.     elif grid[x+1][y-1] == 1:
  52.         return True
  53.     elif grid[x-1][y+1] == 1:
  54.         return True
  55.    
  56.     return False
  57.    
  58. def makeList(size):
  59.     '''Creates a list of size number of zeros'''
  60.    
  61.     mylist = []
  62.     for i in range(size):
  63.         mylist.append(0)
  64.     return mylist
  65.  
  66. def makeMatrix(rows, columns):
  67.     '''Creates a 2D matrix as a list of rows number of lists
  68.       where the lists are columns'''
  69.    
  70.     matrix = []
  71.     for i in range(rows):
  72.         matrix.append(makeList(columns))
  73.     return matrix
  74.  
  75. def randomWalk(start):
  76.     '''Returns a coordinate for a particle randomly moving ONCE
  77.       from its original location (start)'''
  78.        
  79.     #initializes coordinate values
  80.     x = start[0]
  81.     y = start[1]
  82.    
  83.     determinant = random.randrange(1,5)
  84.    
  85.     if determinant == 1:
  86.         y = y+1
  87.     elif determinant == 2:
  88.         y = y-1
  89.     elif determinant == 3:
  90.         x = x+1
  91.     else:
  92.         x = x-1
  93.        
  94.     return [x, y]
  95.  
  96. def numSticks(Matrix):
  97.     '''Counts the number of  particles (ones) in a given matrix'''
  98.    
  99.     count = 0
  100.    
  101.     for row in range(len(Matrix)):
  102.         for column in range(len(Matrix[row])):
  103.             if Matrix[row][column] == 1:
  104.                 count = count+1
  105.                
  106.     return count
  107.  
  108. def dla(particles):
  109.    
  110.     #1. Initialize your turtle graphics window and your matrix.
  111.     t = cTurtle.Turtle()
  112.     t.setWorldCoordinates(0,0,199,199)
  113.     t.up()
  114.     t.color('blue')
  115.     t.hideturtle()
  116.     mx = makeMatrix(200, 200)   #creates a matrix 200-by-200 and assigns it to mx
  117.     origin = [len(mx)//2, len(mx)//2]
  118.    
  119.     #2. Place a seed particle at the origin (both in your matrix and graphically), and set the radius R of the cluster to be 0.
  120.     mx[len(mx)//2][len(mx)//2] = 1
  121.     t.goto(origin[0],origin[1])
  122.     t.dot()
  123.     r = 0
  124.    
  125.     for particle in range(particles):
  126.        
  127.         #3. Release a new particle in a random position at a distance of R + 1 from the origin.
  128.         newPos = origin
  129.         while distance(origin, newPos) != r+1:   #while loop overcomes some error in rounding
  130.             angle = random.randrange(0, 360)
  131.             newPos = findCoordinate(r+1, angle, origin)
  132.  
  133.         #4a. Let the new particle perform a random walk. If, at any step, the particle comes in contact with an existing particle, it "sticks".
  134.         i = 0
  135.         row = newPos[0]
  136.         column = newPos[1]
  137.        
  138.         #4b. Abandon the particle when it wanders outside of a circle with radius that is some function of R.
  139.         while distance(newPos, origin)<2*(r+1) and (adjacent(mx, newPos[0], newPos[1])==False) and row<len(mx) and column<len(mx) and mx[row][column] != 1:   #randomly moves particle until it sticks or goes too far away (includes outside of matrix)
  140.             i = i+1
  141.             newPos = randomWalk(newPos)
  142.             row = newPos[0]
  143.             column = newPos[1]
  144.        
  145.         #4c. Update your matrix, draw a dot at this location, and update R if this particle is further from the origin than the previous radius.
  146.         if distance(newPos, origin)<2*(r+1) and row<len(mx) and column<len(mx):   #if while loop terminated early, signifying a "stick," show where the particle stuck
  147.             if distance(origin, newPos) > r:
  148.                 r = r+1
  149.             t.goto(row,column)
  150.             t.dot()
  151.             mx[row][column] = 1
  152.         #5. Repeat the previous two steps for particles particles.
  153.  
  154.     print("Sticks:", numSticks(mx))
  155.     t.exitOnClick()
  156.  
  157. def main():
  158.     dla(1000)
  159. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement