Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Three Points Circle by apotokin
from math import sqrt
def checkio(points):
# (2,2),(6,2),(2,6)
x1, y1, x2, y2, x3, y3 = (float(f) for f in points.replace('(', "").replace(')', "").split(','))
x12 = x1 - x2
x23 = x2 - x3
x31 = x3 - x1
y12 = y1 - y2
y23 = y2 - y3
y31 = y3 - y1
z1 = x1 * x1 + y1 * y1
z2 = x2 * x2 + y2 * y2
z3 = x3 * x3 + y3 * y3
zx = y12 * z3 + y23 * z1 + y31 * z2
zy = x12 * z3 + x23 * z1 + x31 * z2
z = x12 * y31 - y12 * x31
a=-zx/(2*z)
b= zy/(2*z)
r=sqrt( (a - x1) ** 2 + (b - y1) ** 2)
return "(x-{a:g})^2+(y-{b:g})^2={r:g}^2".format(a=round(a,2),b=round(b,2),r=round(r,2))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == 'MYCODE':
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"
Aug. 12, 2015
Comments: