Advertisement
Ollii

lol 2

Nov 1st, 2011
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. '''
  2. Created on Oct 26, 2011
  3.  
  4. @author:
  5. '''
  6.  
  7. from friend import Friend
  8. from turtle import *
  9.  
  10. class Field(object):
  11.     '''
  12.    Contains a the dimensions of a plane and a list of friends inside the plane.
  13.    Properties are read, calculated and drawn in separate steps.
  14.    '''
  15.  
  16.     def __init__(self):
  17.         '''
  18.        Initialize all attributes
  19.        '''
  20.         # Nothing but imports are written above this line!
  21.            
  22.     def read_friends(self, file_name):
  23.         '''
  24.        Read the properties and friends list.
  25.        '''
  26.         myfilehandle = open(file_name, 'r')
  27.         # extracting the field parameter values from file_name
  28.         field_values_s = myfilehandle.readline()
  29.         self.field_values = field_values_s.split()
  30.         print(self.field_values)
  31.         self.side_x = self.field_values[0]
  32.         self.side_y = self.field_values[1]
  33.         self.friends_in_circle = self.field_values[2]        
  34.        
  35.         # extracting the friend values from file_name
  36.         friend_values_s = myfilehandle.readlines()
  37.         self.friend_values = []
  38.         for i in friend_values_s:
  39.             self.friend_values.append(i[:-1])
  40.         for i in range(len(self.friend_values)):
  41.             self.friend_values[i] = self.friend_values[i].split()
  42.         print(self.friend_values)
  43.        
  44.         myfilehandle.close()
  45.        
  46.     def calculate_friend_distances(self):
  47.         '''
  48.        Calculate the distances between all the friends.
  49.        '''
  50.         self.distances_friends = []
  51.         for i in range(len(self.friend_values)):
  52.             distance_friends = []
  53.             for j in range(len(self.friend_values)):
  54.                         x1 = int(self.friend_values[i][1])
  55.                         x2 = int(self.friend_values[j][1])
  56.                         y1 = int(self.friend_values[i][2])
  57.                         y2 = int(self.friend_values[j][2])
  58.                         distance_friends.append((((x1-x2)**2)+((y1-y2)**2))**0.5)
  59.             self.distances_friends.append(distance_friends)
  60.        
  61.            
  62.     def calculate_friend_circles(self):
  63.         '''
  64.        Calculate the radius for each friend so that each circle contains
  65.        the required amount of friends. The circle intersect the last friend.
  66.        '''
  67.         return
  68.    
  69.     def calculate_closest_friend(self):
  70.         '''
  71.        Determine the friend closest to all others. The total distance traveled
  72.        by all friends is the smallest.
  73.        '''
  74.        
  75.         self.total_distances = []
  76.         for i in self.distances_friends:
  77.             total = 0
  78.             som_temp_list = 0
  79.             temp_list = []
  80.             temp_list.append(i)
  81.             for i in temp_list:
  82.                 som_temp_list += i
  83.                 print(som_temp_list)
  84.            
  85.            
  86.         '''for j in temp_list:
  87.                total += j
  88.                self.total_distances.append(float(total))
  89.            print(float(total))
  90.        print(self.total_distances)'''
  91.  
  92.  
  93.                  
  94.     def init_draw(self):
  95.         '''
  96.        Initialize the screen and turtle.
  97.        '''
  98.         self.turtle = Turtle()
  99.         self.wn = Screen()
  100.         self.wn.title("Friends 'n stuff")
  101.        
  102.     def draw_box(self):
  103.         '''
  104.        Draw a box using the requests x and y sizes.
  105.        '''
  106.         self.turtle.goto(0, 0)
  107.         self.turtle.penup()
  108.         self.turtle.forward(int(self.side_x) / 2)
  109.         self.turtle.pendown()
  110.         self.turtle.left(90)
  111.         self.turtle.forward(int(self.side_y) / 2)
  112.         self.turtle.left(90)
  113.         self.turtle.forward(int(self.side_x))
  114.         self.turtle.left(90)
  115.         self.turtle.forward(int(self.side_y))
  116.         self.turtle.left(90)
  117.         self.turtle.forward(int(self.side_x))
  118.         self.turtle.left(90)
  119.         self.turtle.forward(int(self.side_y) / 2)
  120.    
  121.     def draw_friends(self):
  122.         '''
  123.        Draw a dot on the location of each friend.
  124.        '''  
  125.         for i in range(len(self.friend_values)):
  126.             self.turtle.penup()
  127.             x = int(self.friend_values[i][1])
  128.             y = int(self.friend_values[i][2])
  129.             self.turtle.goto(x, y)
  130.             self.turtle.dot(5, str(self.friend_values[i][3]))
  131.  
  132.        
  133.     def draw_friend_circles(self):
  134.         '''
  135.        Draw a circle around each friends location using the
  136.        calculated radius.
  137.        '''
  138.         return
  139.        
  140.     def draw_closest_friend(self):
  141.         '''
  142.        Draw a black dot on the location of the closest friend.
  143.        '''
  144.         return
  145.    
  146.     def mainloop(self):
  147.         '''
  148.        Keep the screen visible.
  149.        '''
  150.         self.wn.mainloop()
  151.        
  152.        
  153.        
  154.        
  155.  
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement