Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Three Points Circle by Kurush
# migrated from python 2.7
import math
def checkio(data):
coors_str = data.replace("(", "").replace(")", "")
coors = coors_str.split(",")
L = (int(coors[0]), int(coors[1]))
M = (int(coors[2]), int(coors[3]))
N = (int(coors[4]), int(coors[5]))
middleLM = ((L[0] + M[0]) / 2.0, (L[1] + M[1]) / 2.0)
middleMN = ((M[0] + N[0]) / 2.0, (M[1] + N[1]) / 2.0)
A1, B1, C1, line_exists = get_line_equation(L[0], L[1], M[0], M[1])
A2, B2, C2, line_exists = get_line_equation(N[0], N[1], M[0], M[1])
D1, E1, F1 = -B1, A1, B1 * middleLM[0] - A1 * middleLM[1]
D2, E2, F2 = -B2, A2, B2 * middleMN[0] - A2 * middleMN[1]
O = ((F2 * E1 - E2 * F1) / (1.0 * (E2 * D1 - D2 * E1)) , -(F2 * D1 - D2 * F1) / (1.0 * (E2 * D1 - D2 * E1)))
radius = dist(L, O)
equation = "(x-" + str(round(O[0], 2)) + ")^2+(y-" + str(round(O[1], 2)) + ")^2=" + str(round(radius, 2)) + "^2"
equation = equation.replace(".0)", ")")
equation = equation.replace(".0^", "^")
return equation
# Get line equation Ax + By + C = 0 given two points (xa, ya) and (xb, yb).
def get_line_equation(xa, ya, xb, yb):
if (xb - xa != 0) and (yb - ya != 0):
A = 1. / (xb - xa)
B = 1. / (ya - yb)
C = 1. * ya / (yb - ya) - 1. * xa / (xb - xa)
# normalization of koefficients
B = B / A
C = C / A
A = 1.
return A, B, C, True
elif (xb - xa == 0) and (yb - ya != 0):
A = 1.
B = 0.
C = -xa
return A, B, C, True
elif (xb - xa != 0) and (yb - ya == 0):
A = 0.
B = 1.
C = -ya
return A, B, C, True
elif (xb - xa == 0) and (yb - ya == 0):
return 0, 0, 0, False
def dist(a, b):
return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
#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"
June 29, 2014