Advertisement
Ollii

Taak 4 result ubuntu

Nov 1st, 2011
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. '''
  2. Created on Oct 26, 2011
  3.  
  4. @author:
  5. '''
  6.  
  7. from friend import Friend
  8. import turtle
  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.     def calculate_friend_circles(self):
  62.         '''
  63.        Calculate the radius for each friend so that each circle contains
  64.        the required amount of friends. The circle intersect the last friend.
  65.        '''
  66.         self.radius_list = []
  67.         for friend in self.distances_friends:
  68.             friend.sort()
  69.             temp_3rd_friend = friend[3]
  70.             self.radius_list.append(temp_3rd_friend)
  71.         print(self.radius_list)
  72.    
  73.     def calculate_closest_friend(self):
  74.         '''
  75.        Determine the friend closest to all others. The total distance traveled
  76.        by all friends is the smallest.
  77.        '''
  78.        
  79.         total_distances = []
  80.         for friend in self.distances_friends:
  81.                 total = 0
  82.                 for i in friend:
  83.                     total += i
  84.                 total_distances.append(total)
  85.         self.closest_friend = total_distances.index(min(total_distances))
  86.                  
  87.     def init_draw(self):
  88.         '''
  89.        Initialize the screen and turtle.
  90.        '''
  91.         self.turtle = turtle.Turtle()
  92.         self.wn = turtle.Screen()
  93.         self.wn.title("Friends 'n stuff")
  94.         self.turtle.hideturtle()
  95.         self.wn.tracer(0)
  96.        
  97.        
  98.     def draw_box(self):
  99.         '''
  100.        Draw a box using the requests x and y sizes.
  101.        '''
  102.         self.turtle.goto(0, 0)
  103.         self.turtle.penup()
  104.         self.turtle.forward(int(self.side_x) / 2)
  105.         self.turtle.pendown()
  106.         self.turtle.left(90)
  107.         self.turtle.forward(int(self.side_y) / 2)
  108.         self.turtle.left(90)
  109.         self.turtle.forward(int(self.side_x))
  110.         self.turtle.left(90)
  111.         self.turtle.forward(int(self.side_y))
  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) / 2)
  116.    
  117.     def draw_friends(self):
  118.         '''
  119.        Draw a dot on the location of each friend.
  120.        '''  
  121.         for i in range(len(self.friend_values)):
  122.             self.turtle.penup()
  123.             x = int(self.friend_values[i][1])
  124.             y = int(self.friend_values[i][2])
  125.             self.turtle.goto(x, y)
  126.             self.turtle.dot(5, str(self.friend_values[i][3]))
  127.  
  128.        
  129.     def draw_friend_circles(self):
  130.         '''
  131.        Draw a circle around each friends location using the
  132.        calculated radius.
  133.        '''
  134.         for i in range(len(self.friend_values)):
  135.             self.turtle.penup()
  136.             x = int(self.friend_values[i][1])
  137.             y = int(self.friend_values[i][2])
  138.             self.turtle.color(str(self.friend_values[i][3]))
  139.             self.turtle.goto(x+int(self.radius_list[i]),y)
  140.             self.turtle.pendown()
  141.             self.turtle.circle(self.radius_list[i])
  142.        
  143.     def draw_closest_friend(self):
  144.         '''
  145.        Draw a black dot on the location of the closest friend.
  146.        '''
  147.         self.turtle.penup()
  148.         x = int(self.friend_values[int(self.closest_friend)][1])
  149.         y = int(self.friend_values[int(self.closest_friend)][2])
  150.         self.turtle.goto(x,y)
  151.         self.turtle.dot(25, str(self.friend_values[int(self.closest_friend)][3]))
  152.        
  153.    
  154.     def mainloop(self):
  155.         '''
  156.        Keep the screen visible.
  157.        '''
  158.         self.wn.mainloop()
  159.        
  160.        
  161.        
  162.        
  163.  
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement