Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def checkio(data):
- # Get our 3 co-ordinates in integer form
- x1 = int(data[1])
- y1 = int(data[3])
- x2 = int(data[7])
- y2 = int(data[9])
- x3 = int(data[13])
- y3 = int(data[15])
- print ("\n")
- # Check if we have a right angle triangle
- if ((x3 - x1) == 0 or (x2 - x1) == 0 or (x3 - x2) == 0) and ((y3 - y1) == 0 or (y2 - y1) == 0 or (y3 - y2) == 0):
- print ("We have a right angle triangle")
- if (x3 - x1) == 0:
- h = (max(x2,x3) + min(x2,x3)) / 2
- bestX = x2
- secondX = x1
- elif (x2 - x1) == 0:
- h = (max(x2,x3) + min(x2,x3)) / 2
- bestX = x3
- secondX = x1
- elif (x3 - x2) == 0:
- h = (max(x1,x3) + min(x1,x3)) / 2
- bestX = x1
- secondX = x2
- if (y3 - y1) == 0:
- k = (max(y2,y3) + min(y2,y3)) / 2
- bestY = y2
- secondY = y1
- elif (y2 - y1) == 0:
- k = (max(y2,y3) + min(y2,y3)) / 2
- bestY = y3
- secondY = y1
- elif (y3 - y2) == 0:
- k = (max(y1,y3) + min(y1,y3)) / 2
- bestY = y1
- secondY = y3
- # Find the radius (r)
- r = (((max(x1,x2,x3) - h) ** 2) + ((max(y1,y2,y3) - k) ** 2)) ** 0.5
- # If not right angle triangle: Find the slopes of the two lines (mr and mt)
- else:
- mr = (y2 - y1) / (x2 - x1)
- mt = (y3 - y2) / (x3 - x2)
- if mr == 0:
- mr = (y3 - y1) / (x3 - x1)
- elif mt == 0:
- mt = (y3 - y1) / (x3 - x1)
- # Find the x-coordinate of the circle (h)
- h = ((mr * mt * (y3 - y1)) + (mr * (x2 + x3)) - (mt * (x1 + x2))) / (2 * (mr - mt))
- # Find the y-coordinate of the circle (k)
- k = (((-1) / mr) * (h - ((x1 + x2) / 2))) + ((y1 + y2) / 2)
- # Find the radius of the circle (r)
- r = (((h - x3) ** 2) + ((k - y3) ** 2)) ** 0.5
- h = round(h,2)
- k = round(k,2)
- r = round(r,2)
- if int(h) == h:
- h = int(h)
- if int(k) == k:
- k = int(k)
- equation = "(x-"+str(h)+")^2+(y-"+str(k)+")^2="+str(r)+"^2"
- print ("Answer:")
- return equation
- print(checkio("(2,2),(1,4),(2,5)")) == "(x-2.5)^2+(y-3.5)^2=1.58^2"
- print(checkio("(7,7),(4,3),(1,8)")) == "(x-3.8)^2+(y-6.28)^2=3.28^2"
- print(checkio("(2,2),(4,2),(2,4)")) == "(x-3)^2+(y-3)^2=1.41^2"
- print(checkio("(2,2),(6,2),(2,6)")) == "(x-4)^2+(y-4)^2=2.83^2"
- print(checkio("(3,7),(6,9),(9,7)")) == "(x-6)^2+(y-5.75)^2=3.25^2"
- 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