forstjiri

Biggest triangl

Oct 22nd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. import math
  2.  
  3. def get_triangle(a, b, c, d):
  4.             '''
  5.            @param: celkem 4 body ve formatu (x, y)
  6.            @return: 3 body ktere tvori nejvetsi trojuhelnik
  7.            '''
  8.             t = []
  9.             trojuhelniky = [ [a, b, c], [a, b, d], [a, c, d], [b, c, d] ]
  10.             for troj in trojuhelniky:
  11.                   p1 = trojuhelnik(troj[0], troj[1], troj[2])
  12.                   if p1.jeTrojuhelnik():
  13.                         t.append(p1)
  14.                        
  15.             biggest = t[0]            
  16.             for i in range(1, len(t)):
  17.                   if (t[i].obsah > biggest.obsah):
  18.                             biggest = t[i]            
  19.             return biggest.b1, biggest.b2, biggest.b3, biggest.obsah
  20.  
  21. class bod:
  22.     '''
  23.    datovy typ pro bod
  24.    '''
  25.     def __init__(self, x, y):
  26.         self.x = x
  27.         self.y = y
  28.        
  29.     def __repr__(self):
  30.                 return repr((self.x, self.y))
  31.  
  32. class trojuhelnik:
  33.       '''
  34.      datovy typ pro trojuhelnik
  35.      '''      
  36.       def __init__(self, b1, b2, b3):
  37.             self.b1 = b1
  38.             self.b2 = b2
  39.             self.b3 = b3
  40.            
  41.             self.s1 = self.vzdalenostDvouBodu(b1, b2)
  42.             self.s2 = self.vzdalenostDvouBodu(b2, b3)
  43.             self.s3 = self.vzdalenostDvouBodu(b3, b1)
  44.            
  45.             self.obsah = ( (self.s1 * self.s2 * self.s3) / 2 )
  46.            
  47.             self.je = self.jeTrojuhelnik()
  48.            
  49.       def __repr__(self):
  50.                 return repr((self.obsah, self.s1, self.s2, self.s3, self.b1, self.b2, self.b3))
  51.      
  52.       def vzdalenostDvouBodu(self, b1, b2):
  53.             '''
  54.            Zjisti jak daleko jsou od sebe dva body
  55.            '''
  56.             return math.sqrt( math.pow(b1.x - b2.x, 2) + math.pow(b1.y - b2.y, 2) )
  57.      
  58.       def jeTrojuhelnik(self):
  59.             '''
  60.            zjisti zda je to trojuhenik
  61.            '''
  62.             strany = [self.s1, self.s2, self.s3]
  63.             strany.sort(reverse = True)
  64.             if (strany[0] < (strany[1] + strany[2]) ):
  65.                   return True
  66.             else:
  67.                   return False
Advertisement
Add Comment
Please, Sign In to add comment