Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Three Points Circle by colinmcnicholl
def checkio(data):
"""Input: Coordinates as a string.
The function finds the circle for three given points.
Use equations from Circumcenter co-ordinates section of
https://en.wikipedia.org/wiki/Circumscribed_circle
Output: The equation of the circle as a string.
"""
(A_x, A_y), (B_x, B_y), (C_x, C_y) = eval(data)
D = 2*(A_x * (B_y - C_y) + B_x * (C_y - A_y) + C_x * (A_y - B_y))
U_x = ((1 / D) * ((A_x ** 2 + A_y ** 2) * (B_y - C_y)
+ (B_x ** 2 + B_y ** 2) * (C_y - A_y)
+ (C_x ** 2 + C_y ** 2) * (A_y - B_y)))
U_y = ((1 / D) * ((A_x ** 2 + A_y ** 2) * (C_x - B_x)
+ (B_x ** 2 + B_y ** 2) * (A_x - C_x)
+ (C_x ** 2 + C_y ** 2) * (B_x - A_x)))
radius = round((((A_x - U_x) ** 2 + (A_y - U_y) ** 2) ** 0.5), 2)
return "(x-{:g})^2+(y-{:g})^2={:g}^2".format(
round(U_x, 2), round(U_y, 2), radius)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio("(2,2),(6,2),(2,6)") == "(x-4)^2+(y-4)^2=2.83^2"
assert checkio("(3,7),(6,9),(9,7)") == "(x-6)^2+(y-5.75)^2=3.25^2"
Feb. 16, 2019
Comments: