Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. def checkio(data):
  2.     # Get our 3 co-ordinates in integer form
  3.     x1 = int(data[1])
  4.     y1 = int(data[3])
  5.     x2 = int(data[7])
  6.     y2 = int(data[9])
  7.     x3 = int(data[13])
  8.     y3 = int(data[15])
  9.     print ("\n")
  10.  
  11.     # Check if we have a right angle triangle
  12.     if ((x3 - x1) == 0 or (x2 - x1) == 0 or (x3 - x2) == 0) and ((y3 - y1) == 0 or (y2 - y1) == 0 or (y3 - y2) == 0):
  13.         print ("We have a right angle triangle")
  14.         if (x3 - x1) == 0:
  15.             h = (max(x2,x3) + min(x2,x3)) / 2
  16.             bestX = x2
  17.             secondX = x1
  18.         elif (x2 - x1) == 0:
  19.             h = (max(x2,x3) + min(x2,x3)) / 2
  20.             bestX = x3
  21.             secondX = x1
  22.         elif (x3 - x2) == 0:
  23.             h = (max(x1,x3) + min(x1,x3)) / 2
  24.             bestX = x1
  25.             secondX = x2
  26.         if (y3 - y1) == 0:
  27.             k = (max(y2,y3) + min(y2,y3)) / 2
  28.             bestY = y2
  29.             secondY = y1
  30.         elif (y2 - y1) == 0:
  31.             k = (max(y2,y3) + min(y2,y3)) / 2
  32.             bestY = y3
  33.             secondY = y1
  34.         elif (y3 - y2) == 0:
  35.             k = (max(y1,y3) + min(y1,y3)) / 2
  36.             bestY = y1
  37.             secondY = y3
  38.            
  39.         # Find the radius (r)
  40.         r = (((max(x1,x2,x3) - h) ** 2) + ((max(y1,y2,y3) - k) ** 2)) ** 0.5
  41.            
  42.     # If not right angle triangle: Find the slopes of the two lines (mr and mt)
  43.     else:
  44.         mr = (y2 - y1) / (x2 - x1)
  45.         mt = (y3 - y2) / (x3 - x2)
  46.         if mr == 0:
  47.             mr = (y3 - y1) / (x3 - x1)
  48.         elif mt == 0:
  49.             mt = (y3 - y1) / (x3 - x1)
  50.        
  51.         # Find the x-coordinate of the circle (h)
  52.         h = ((mr * mt * (y3 - y1)) + (mr * (x2 + x3)) - (mt * (x1 + x2))) / (2 * (mr - mt))
  53.         # Find the y-coordinate of the circle (k)
  54.         k = (((-1) / mr) * (h - ((x1 + x2) / 2))) + ((y1 + y2) / 2)
  55.  
  56.         # Find the radius of the circle (r)
  57.         r = (((h - x3) ** 2) + ((k - y3) ** 2)) ** 0.5
  58.  
  59.     h = round(h,2)
  60.     k = round(k,2)
  61.     r = round(r,2)
  62.    
  63.     if int(h) == h:
  64.         h = int(h)
  65.     if int(k) == k:
  66.         k = int(k)
  67.  
  68.     equation = "(x-"+str(h)+")^2+(y-"+str(k)+")^2="+str(r)+"^2"
  69.     print ("Answer:")
  70.     return equation
  71.  
  72. print(checkio("(2,2),(1,4),(2,5)")) == "(x-2.5)^2+(y-3.5)^2=1.58^2"
  73. print(checkio("(7,7),(4,3),(1,8)")) == "(x-3.8)^2+(y-6.28)^2=3.28^2"
  74. print(checkio("(2,2),(4,2),(2,4)")) == "(x-3)^2+(y-3)^2=1.41^2"
  75. print(checkio("(2,2),(6,2),(2,6)")) == "(x-4)^2+(y-4)^2=2.83^2"
  76. print(checkio("(3,7),(6,9),(9,7)")) == "(x-6)^2+(y-5.75)^2=3.25^2"
  77. print(checkio("(7,3),(9,6),(3,6)")) == "(x-6)^2+(y-5.83)^2=3^2"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement