Problem:
Circumscribed Circle of A Triangle. Write a program which prints the central coordinate $(p_x, p_y)$ and the radius $r$ of a circumscribed circle of a triangle which is constructed by three points $(x_1, y_1)$, $(x_2, y_2)$ and $(x_3, y_3)$ on the plane surface. Input Input consists of several datasets. In the first line, the number of datasets $n$ is given. Each dataset consists of:  $x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$  in a line. All the input are real numbers. Output For each dataset, print $p_x$, $p_y$ and $r$ separated by a space in a line. Print the solution to three places of decimals. Round off the solution to three decimal places. Constraints  $-100 \leq x_1, y_1, x_2, y_2, x_3, y_3 \leq 100$  $ n \leq 20$  Sample Input 1
0.0 0.0 2.0 0.0 2.0 2.0 Output for the Sample Input 1.000 1.000 1.414

Solution (Python):
import math

class P(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def width(self, p):
        return math.sqrt((self.x - p.x)**2 + (self.y - p.y)**2)

    def __repr__(self):
        return '{0:.3f} {1:.3f}'.format(self.x, self.y)

def calc_cos(a,b,c):
    return (b**2 + c**2 - a**2) / (2*b*c)

def calc_sin(c):
    return math.sqrt(1 - c**2)

def calc_2sin(s,c):
    return 2 * s * c

def run():
    n = int(input())
    for _ in range(n):
        x1, y1, x2, y2, x3, y3 = list(map(float, input().split()))
        p1, p2, p3 = P(x1, y1), P(x2, y2), P(x3, y3)
        a, b, c = p1.width(p2), p2.width(p3), p3.width(p1)
        cosA, cosB, cosC = calc_cos(a,b,c), calc_cos(b,c,a), calc_cos(c,a,b)
        sinA, sinB, sinC = calc_sin(cosA), calc_sin(cosB), calc_sin(cosC)
        sin2A, sin2B, sin2C = calc_2sin(sinA, cosA), calc_2sin(sinB, cosB), calc_2sin(sinC, cosC)

        r = a / sinA / 2

        x = (p1.x * sin2B + p2.x * sin2C + p3.x * sin2A) / (sin2A + sin2B + sin2C)
        y = (p1.y * sin2B + p2.y * sin2C + p3.y * sin2A) / (sin2A + sin2B + sin2C)

        print('{0:.3f} {1:.3f} {2:.3f}'.format(x,y,r))

if __name__ == '__main__':
    run()

Solution (Python):


import cmath

class Point(object):
    def __init__(self, x, y):
        self.point = complex(x, y)
    def __str__(self):
        return "x = {0}, y = {1}".format(self.point.real, self.point.imag)

class Triangle(Point):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        
        self.edgeA = abs(b.point-c.point)
        self.edgeB = abs(c.point-a.point)
        self.edgeC = abs(a.point-b.point)
        
        self.angleA = Triangle.angle(self.edgeA, self.edgeB, self.edgeC)
        self.angleB = Triangle.angle(self.edgeB, self.edgeC, self.edgeA)
        self.angleC = Triangle.angle(self.edgeC, self.edgeA, self.edgeB)

    
    def angle(A, B, C):
        return cmath.acos( (B*B+C*C-A*A)/(2*B*C) )


    
    def circumscribedCircleRadius(self):
        return abs((self.edgeA/cmath.sin(self.angleA))/2)

    
    def circumscribedCircleCenter(self):
        A = cmath.sin(2*self.angleA)
        B = cmath.sin(2*self.angleB)
        C = cmath.sin(2*self.angleC)
        X = (self.a.point.real*A + self.b.point.real*B + self.c.point.real*C) / (A+B+C)
        Y = (self.a.point.imag*A + self.b.point.imag*B + self.c.point.imag*C) / (A+B+C)
        return complex(X, Y)

n = int(input())
for i in range(n):
    line = list(map(float, input().split()))
    p1 = Point(line[0], line[1])
    p2 = Point(line[2], line[3])
    p3 = Point(line[4], line[5])
    T = Triangle(p1, p2, p3)
    center = T.circumscribedCircleCenter()
    print("{0:.3f} {1:.3f} {2:.3f}".format(center.real, center.imag, T.circumscribedCircleRadius()))

Solution (Python):
import math
n = input()
for i in range(n):
    p = map(float, raw_input().split())
    x1, y1, x2, y2, x3, y3 = p[0], p[1], p[2], p[3], p[4], p[5]
    
    px = ((y1- y3) * (y1**2 - y2**2 + x1**2 - x2**2) - (y1 - y2) * (y1**2 - y3**2 + x1**2 - x3**2)) / (2 * (y1 - y3) * (x1 - x2) - 2 * (y1 - y2) * (x1 - x3))
    py = ((x1- x3) * (x1**2 - x2**2 + y1**2 - y2**2) - (x1 - x2) * (x1**2 - x3**2 + y1**2 - y3**2)) / (2 * (x1 - x3) * (y1 - y2) - 2 * (x1 - x2) * (y1 - y3))
    r = math.sqrt((x1 - px)**2 + (y1 - py)**2)
    print("%.3f %.3f %.3f" % (px, py, r))

Solution (Python):



import sys

def calc_det(lis):
  return lis[0]*lis[3]-lis[1]*lis[2]

def sq(x):
  return x*x

for s in sys.stdin:
  d = map(float, s.split() )
  if len(d) == 1:
    continue
  x1,y1,x2,y2,x3,y3 = d[0],d[1],d[2],d[3],d[4],d[5]
  d11 = 2*(x3-x2)
  d12 = 2*(y3-y2)
  d21 = 2*(x2-x1)
  d22 = 2*(y2-y1)
  x11 = sq(x3)-sq(x2)+sq(y3)-sq(y2)
  x21 = sq(x2)-sq(x1)+sq(y2)-sq(y1)
  y12 = x11
  y22 = x21

  x0 = calc_det( [x11,d12,x21,d22] )/calc_det( [d11,d12,d21,d22] )
  y0 = calc_det( [d11,y12,d21,y22] )/calc_det( [d11,d12,d21,d22] )

  r = ( (x0-x1)**2 + (y0-y1)**2 )**0.5

  print "%.3f %.3f %.3f" % (x0,y0,r)

'''
Bibliography
３点を通る円の半径を求む（
http://www.geocities.jp/jtqsw192/FIG/313r/3point_r.htm
'''

Solution (Python):
import sys
f = sys.stdin

def take2(iterable):
    while True:
        yield next(iterable), next(iterable)

def cross(v1, v2):
    return v1.real * v2.imag - v1.imag * v2.real

def get_intersection(p1,p2,p3,p4):
    a1 = p4 - p2
    b1 = p2 - p3
    b2 = p1 - p2
    s1 = cross(a1, b2) / 2
    s2 = cross(a1, b1) / 2
    return p1 + (p3 - p1) * s1 / (s1 + s2)

n = int(f.readline())
for i in range(n):
    p1, p2, p3 = [x + y * 1j for x, y in take2(map(float, f.readline().split()))]
    p12 = (p1 + p2) / 2
    p13 = (p1 + p3) / 2
    pxy = get_intersection(p12,p13,p12 + (p2 - p1) * 1j,p13 + (p1 - p3) * 1j)
    r = abs(pxy - p1)
    print('{:.3f} {:.3f} {:.3f}'.format(pxy.real,pxy.imag,r))

Solution (Python):
import math
for i in range(input()):
    x1,y1,x2,y2,x3,y3=map(float,raw_input().split(" "))
    if y2==y1 or y3==y1:
        if y2==y1:
            a2=-(x3-x1)/(y3-y1)
            b2=((y3+y1)-a2*(x1+x3))/2.0
            a,b,c,d,e,f=1.0,0.0,(x1+x2)/2.0,-a2,1.0,b2
        else:
            a1=-(x2-x1)/(y2-y1)
            b1=((y2+y1)-a1*(x1+x2))/2.0
            a,b,c,d,e,f=-a1,1.0,b1,1.0,0.0,(x1+x3)/2.0
    else:
        a1=-(x2-x1)/(y2-y1)
        a2=-(x3-x1)/(y3-y1)
        b1=((y2+y1)-a1*(x1+x2))/2.0
        b2=((y3+y1)-a2*(x1+x3))/2.0
        a,b,c,d,e,f=-a1,1.0,b1,-a2,1.0,b2
    py=(a*f-c*d)/(a*e-b*d)
    px=(c*e-f*b)/(a*e-b*d)
    r=math.sqrt((px-x1)**2 + (py-y1)**2)
    print "{0:.3f} {1:.3f} {2:.3f}".format(px,py,r)

Solution (Python):

import math
for i in range(int(raw_input())):
    x1, y1, x2, y2, x3, y3 = map(float,raw_input().split())
    px = ((y1-y3)*(y1**2 -y2**2 +x1**2 -x2**2) -(y1-y2)*(y1**2 -y3**2 +x1**2 -x3**2)) / (2*(y1-y3)*(x1-x2)-2*(y1-y2)*(x1-x3))
    py = ((x1-x3)*(x1**2 -x2**2 +y1**2 -y2**2) -(x1-x2)*(x1**2 -x3**2 +y1**2 -y3**2)) / (2*(x1-x3)*(y1-y2)-2*(x1-x2)*(y1-y3))
    r = math.sqrt((x1-px)**2 + (y1-py)**2)
    print "%.3f %.3f %.3f" % (px, py, r)

Solution (Python):
def calc(l):
	A1=2*(l[1][0]-l[0][0])
	B1=2*(l[1][1]-l[0][1])
	C1=l[0][0]**2-l[1][0]**2+l[0][1]**2-l[1][1]**2
	A2=2*(l[2][0]-l[0][0])
	B2=2*(l[2][1]-l[0][1])
	C2=l[0][0]**2-l[2][0]**2+l[0][1]**2-l[2][1]**2
	X=(B1*C2-B2*C1)/(A1*B2-A2*B1)
	Y=(C1*A2-C2*A1)/(A1*B2-A2*B1)
	R=((X-l[0][0])**2+(Y-l[0][1])**2)**0.5
	return tuple(map(round, [X,Y,R], [3]*3))
l=[zip(*[iter(map(float,raw_input().split()))]*2) for i in range(input())]
for ll in l:
	print "%.3f %.3f %.3f"%(calc(ll))

Solution (Python):
def calc(a,b,c,d,e,f):
	A1=2*(c-a)
	B1=2*(d-b)
	C1=a**2-c**2+b**2-d**2
	A2=2*(e-a)
	B2=2*(f-b)
	C2=a*a-e*e+b*b-f*f
	X=(B1*C2-B2*C1)/(A1*B2-A2*B1)
	Y=(C1*A2-C2*A1)/(A1*B2-A2*B1)
	R=((X-a)**2+(Y-b)**2)**0.5
	return tuple(map(round, [X,Y,R], [3]*3))
l=[map(float,raw_input().split()) for i in range(input())]
for ll in l:
	print "%.3f %.3f %.3f"%(calc(*ll))

Solution (Python):
def calc(a,b,c,d,e,f):
	A=2*(c-a)
	B=2*(d-b)
	C=a*a-c*c+b*b-d*d
	D=2*(e-a)
	E=2*(f-b)
	F=a*a-e*e+b*b-f*f
	N=(A*E-D*B)
	X=(B*F-E*C)/N
	Y=(C*D-F*A)/N
	R=((X-a)**2+(Y-b)**2)**0.5
	return tuple(map(round,[X,Y,R],[3]*3))
l=[map(float,raw_input().split())for i in range(input())]
for k in l:print"%.3f %.3f %.3f"%(calc(*k))

Solution (Python):
def calc(a,b,c,d,e,f):
	A=2*(c-a)
	B=2*(d-b)
	C=a*a-c*c+b*b-d*d
	D=2*(e-a)
	E=2*(f-b)
	F=a*a-e*e+b*b-f*f
	N=(A*E-D*B)
	X=(B*F-E*C)/N
	Y=(C*D-F*A)/N
	return tuple(map(round,[X,Y,((X-a)**2+(Y-b)**2)**0.5],[3]*3))
l=[map(float,raw_input().split())for i in range(input())]
for k in l:print"%.3f %.3f %.3f"%(calc(*k))

Solution (Python):
def g(a,b,c,d,e,f):
	A=2*(c-a)
	B=2*(d-b)
	C=a*a-c*c+b*b-d*d
	D=2*(e-a)
	E=2*(f-b)
	F=a*a-e*e+b*b-f*f
	N=(A*E-D*B)
	X=(B*F-E*C)/N
	Y=(C*D-F*A)/N
	return tuple(map(round,[X,Y,((X-a)**2+(Y-b)**2)**0.5],[3]*3))
for k in [map(float,raw_input().split())for i in range(input())]:print"%.3f %.3f %.3f"%(g(*k))

Solution (Python):



import sys
import math

n = int(input())

for data in sys.stdin:
    x1, y1, x2, y2, x3, y3 = map(float, data.split())
    a1 = x2 - x1
    b1 = y2 - y1
    a2 = x3 - x1
    b2 = y3 - y1
    px = (b2 * (a1 * a1 + b1 * b1) - b1 * (a2 * a2 + b2 * b2)) / (2 * (a1 * b2 - a2 * b1))
    py = (a1 * (a2 * a2 + b2 * b2) - a2 * (a1 * a1 + b1 * b1)) / (2 * (a1 * b2 - a2 * b1))
    r = math.sqrt(px * px + py * py)
    px += x1
    py += y1
    print("{0:.3f} {1:.3f} {2:.3f}".format(px, py, r))

Solution (Python):





def __det(matrix):
    return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

def __sqdist(p1,p2):
    return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**.5

n = int(raw_input())
for i in range(n):
    x1,y1,x2,y2,x3,y3 = map(float,raw_input().split(' '))
    det = __det([[(x2-x1),(y2-y1)],[(x3-x1),(y3-y1)]])
    c1 = (x1**2-x2**2+y1**2-y2**2)/2
    c2 = (x1**2-x3**2+y1**2-y3**2)/2
    px = __det([[c1,(y1-y2)],[c2,(y1-y3)]])/det
    py = __det([[(x1-x2),c1],[(x1-x3),c2]])/det
    r = __sqdist([x1,y1],[px,py])
    print '{0:.3f} {1:.3f} {2:.3f}'.format(px,py,r)

Solution (Python):





def __det(matrix):
    return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

def __sqdist(p1,p2):
    return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**.5

n = int(raw_input())
for i in range(n):
    x1,y1,x2,y2,x3,y3 = map(float,raw_input().split(' '))
    det = __det([[(x2-x1),(y2-y1)],[(x3-x1),(y3-y1)]])
    c1 = (x2**2-x1**2+y2**2-y1**2)/2
    c2 = (x3**2-x1**2+y3**2-y1**2)/2
    px = __det([[c1,(y2-y1)],[c2,(y3-y1)]])/det
    py = __det([[(x2-x1),c1],[(x3-x1),c2]])/det
    r = __sqdist([x1,y1],[px,py])
    print '{0:.3f} {1:.3f} {2:.3f}'.format(px,py,r)

Solution (Python):
n=int(raw_input())
for i in range(n):
    x1,y1,x2,y2,x3,y3=map(float,raw_input().split())
    a=((x1-x2)**2+(y1-y2)**2)**0.5
    b=((x1-x3)**2+(y1-y3)**2)**0.5
    c=((x2-x3)**2+(y2-y3)**2)**0.5
    s=(a+b+c)/2
    ss=(s*(s-a)*(s-b)*(s-c))**0.5
    sina=2*ss/b/c
    r=a/sina/2
    a*=a
    b*=b
    c*=c
    px=(a*(b+c-a)*x3+b*(a+c-b)*x2+c*(a+b-c)*x1)/16/ss**2
    py=(a*(b+c-a)*y3+b*(a+c-b)*y2+c*(a+b-c)*y1)/16/ss**2

    print '%.3f %.3f %.3f'%(px,py,r)

Solution (Python):
import math
for i in range(input()):
    x1,y1,x2,y2,x3,y3=map(float,raw_input().split(" "))
    if y2==y1 or y3==y1:
        if y2==y1:
            a2=-(x3-x1)/(y3-y1)
            b2=((y3+y1)-a2*(x1+x3))/2.0
            a,b,c,d,e,f=1.0,0.0,(x1+x2)/2.0,-a2,1.0,b2
        else:
            a1=-(x2-x1)/(y2-y1)
            b1=((y2+y1)-a1*(x1+x2))/2.0
            a,b,c,d,e,f=-a1,1.0,b1,1.0,0.0,(x1+x3)/2.0
    else:
        a1=-(x2-x1)/(y2-y1)
        a2=-(x3-x1)/(y3-y1)
        b1=((y2+y1)-a1*(x1+x2))/2.0
        b2=((y3+y1)-a2*(x1+x3))/2.0
        a,b,c,d,e,f=-a1,1.0,b1,-a2,1.0,b2
    py=(a*f-c*d)/(a*e-b*d)
    px=(c*e-f*b)/(a*e-b*d)
    r=math.sqrt((px-x1)**2 + (py-y1)**2)
    print "{0:.3f} {1:.3f} {2:.3f}".format(px,py,r)

Solution (Python):
import math

n = int(input())
for i in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    a1 = x1 ** 2 - x2 ** 2 + y1 ** 2 - y2 ** 2
    a2 = y1 ** 2 - y3 ** 2 + x1 ** 2 - x3 ** 2
    px = ((y1 - y3) * a1 - (y1 - y2) * a2) / \
        (2 * (y1 - y3) * (x1 - x2) - 2 * (y1 - y2) * (x1 - x3))
    py = ((x1 - x3) * a1 - (x1 - x2) * a2) / \
        (2 * (x1 - x3) * (y1 - y2) - 2 * (x1 - x2) * (y1 - y3))
    r = math.hypot(x1 - px, y1 - py)
    print("{0:.3f} {1:.3f} {2:.3f}".format(px, py, r))

Solution (Python):
N = int(input()) 
for i in range(N):
    
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    
    a = complex(x1, y1)
    b = complex(x2, y2)
    c = complex(x3, y3)
    
    a -= c
    b -= c
    
    z0 = abs(a)**2 * b - abs(b)**2 * a
    z0 /= a.conjugate() * b - a * b.conjugate()
    
    
    z = z0 + c
    zx = "{0:.3f}".format(z.real)
    zy = "{0:.3f}".format(z.imag)
    r  = "{0:.3f}".format(abs(z0))
    print(zx, zy, r)

Solution (Python):

import math
n = input()
for x in xrange(n):
    x1,y1,x2,y2,x3,y3 = map(float,raw_input().split())
    a1 = 2.0*(x2-x1)
    b1 = 2.0*(y2-y1)
    x12 = x1**2
    y12 = y1**2
    c1 = x12-x2**2+y12-y2**2
    a2 = 2.0*(x3-x1)
    b2 = 2.0*(y3-y1)
    c2 = x12-x3**2+y12-y3**2
    denom=(a1*b2-a2*b1)
    
    
    
    
    
    
    
    
    
    x = (b1*c2-b2*c1)/denom
    y = (c1*a2-c2*a1)/denom
    r = math.sqrt((x-x1)**2+(y-y1)**2)

    print "%.3f %.3f %.3f"%(x,y,r)

Solution (Python):
import math
for i in range(input()):
    x1,y1,x2,y2,x3,y3=map(float,raw_input().split(" "))
    if y2==y1 or y3==y1:
        if y2==y1:
            a2=-(x3-x1)/(y3-y1)
            b2=((y3+y1)-a2*(x1+x3))/2.0
            a,b,c,d,e,f=1.0,0.0,(x1+x2)/2.0,-a2,1.0,b2
        else:
            a1=-(x2-x1)/(y2-y1)
            b1=((y2+y1)-a1*(x1+x2))/2.0
            a,b,c,d,e,f=-a1,1.0,b1,1.0,0.0,(x1+x3)/2.0
    else:
        a1=-(x2-x1)/(y2-y1)
        a2=-(x3-x1)/(y3-y1)
        b1=((y2+y1)-a1*(x1+x2))/2.0
        b2=((y3+y1)-a2*(x1+x3))/2.0
        a,b,c,d,e,f=-a1,1.0,b1,-a2,1.0,b2
    py=(a*f-c*d)/(a*e-b*d)
    px=(c*e-f*b)/(a*e-b*d)
    r=math.sqrt((px-x1)**2 + (py-y1)**2)
    print "{0:.3f} {1:.3f} {2:.3f}".format(px,py,r)

Solution (Python):
for i in range(int(input())):
    x1, y1, x2, y2, x3, y3 = list(map(float,input().split()))

    a = x2 - x1
    b = y2 - y1
    c = (x1**2 - x2**2) + (y1**2 - y2**2)

    d = x3 - x1
    e = y3 - y1
    f = (x1**2 - x3**2) + (y1**2 - y3**2)

    l = (e*c - b*f) / (a*e - b*d)
    m = (c*d - f*a) / (b*d - a*e)
    n = -(x1**2 + y1**2 + l*x1 + m*y1)

    import math
    px = round(-l/2, 3)
    py = round(-m/2, 3)
    r = round(math.sqrt(l**2 + m**2 - 4*n ) / 2, 3)
    print("{:.3f} {:.3f} {:.3f}".format(px, py, r))

Solution (Python):
def circum(x1,y1,x2,y2,x3,y3):
    a1 = 2 * (x2 - x1)
    b1 = 2 * (y2 - y1)
    c1 = x1 ** 2 - x2 ** 2 + y1 ** 2 - y2 ** 2
    a2 = 2 * (x3 - x1)
    b2 = 2 * (y3 - y1)
    c2 = x1 ** 2 - x3 ** 2 + y1 ** 2 - y3 ** 2
    X = (b1 * c2 - b2 * c1)/(a1 * b2 - a2 * b1)
    Y = (c1 * a2 - c2 * a1)/(a1 * b2 - a2 * b1)
    R = ((x1 - X) ** 2 + (y1 - Y) ** 2) ** 0.5
    return map(lambda n: round(n,3), [X,Y,R])
N = int(input())
ans = []
for i in range(N):
    ans.append(circum(*list(map(float,input().split()))))
for i in range(N):
    print('{0:.3f} {1:.3f} {2:.3f}'.format(*ans[i]))

Solution (Python):
import sys
 
def calc_det(lis):
  return lis[0]*lis[3]-lis[1]*lis[2]
 
def sq(x):
  return x*x
 
for s in sys.stdin:
  d = map(float, s.split() )
  if len(d) == 1:
    continue
  x1,y1,x2,y2,x3,y3 = d[0],d[1],d[2],d[3],d[4],d[5]
  d11 = 2*(x3-x2)
  d12 = 2*(y3-y2)
  d21 = 2*(x2-x1)
  d22 = 2*(y2-y1)
  x11 = sq(x3)-sq(x2)+sq(y3)-sq(y2)
  x21 = sq(x2)-sq(x1)+sq(y2)-sq(y1)
  y12 = x11
  y22 = x21
 
  x0 = calc_det( [x11,d12,x21,d22] )/calc_det( [d11,d12,d21,d22] )
  y0 = calc_det( [d11,y12,d21,y22] )/calc_det( [d11,d12,d21,d22] )
 
  r = ( (x0-x1)**2 + (y0-y1)**2 )**0.5
 
  print "%.3f %.3f %.3f" % (x0,y0,r)

Solution (Python):


 
import sys
import math
 
n = int(input())
 
for data in sys.stdin:
    x1, y1, x2, y2, x3, y3 = map(float, data.split())
    a1 = x2 - x1
    b1 = y2 - y1
    a2 = x3 - x1
    b2 = y3 - y1
    px = (b2 * (a1 * a1 + b1 * b1) - b1 * (a2 * a2 + b2 * b2)) / (2 * (a1 * b2 - a2 * b1))
    py = (a1 * (a2 * a2 + b2 * b2) - a2 * (a1 * a1 + b1 * b1)) / (2 * (a1 * b2 - a2 * b1))
    r = math.sqrt(px * px + py * py)
    px += x1
    py += y1
    print("{0:.3f} {1:.3f} {2:.3f}".format(px, py, r))

Solution (Python):


import math

class Point_Class():
    def __init__(self, x, y):
        self.x = x
        self.y = y

def calcCenter(p1, p2, p3):
    p = ((p1.y-p2.y)*(p3.x*p3.x+p1.y*p2.y)+(p2.y-p3.y)*(p1.x*p1.x+p2.y*p3.y)+(p3.y-p1.y)*(p2.x*p2.x+p3.y*p1.y)) / ((-2)*(p1.y*(p2.x-p3.x)+p2.y*(p3.x-p1.x)+p3.y*(p1.x-p2.x)))
    q = ((p1.x-p2.x)*(p3.y*p3.y+p1.x*p2.x)+(p2.x-p3.x)*(p1.y*p1.y+p2.x*p3.x)+(p3.x-p1.x)*(p2.y*p2.y+p3.x*p1.x)) / ((-2)*(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y)))
    return Point_Class(p, q)

def calcRadius(p, pc):
    return math.sqrt(pow((p.x-pc.x), 2)+pow((p.y-pc.y), 2))

n = int(raw_input())
for i in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, raw_input().split())
    p1 = Point_Class(x1, y1)
    p2 = Point_Class(x2, y2)
    p3 = Point_Class(x3, y3)
    pc = calcCenter(p1, p2, p3)
    r = calcRadius(p1, pc)
    print "%.3f %.3f %.3f" %(pc.x, pc.y, r)

Solution (Python):
def perpendicular_bisector(p, q):
    x = (q[0] - p[0])
    y = (q[1] - p[1])
    return (2 * x, 2 * y, p[0]**2-q[0]**2+p[1]**2-q[1]**2)


def gauss_jordan_elimination(Array):
    
    N = len(Array)
    if N == 0:
        return (True, Array)
    else:
        M = len(Array[0])

    A = []
    for i in range(len(Array)):
        A.append(Array[i][:])

    pivot = 0
    L = min(N, M)
    while pivot < L:
        pivot_v = A[pivot][pivot]
        pivot_row = pivot
        for i in range(pivot + 1, L):
            v = max(A[i][pivot], -A[i][pivot])
            if pivot_v < v:
                pivot_row = i
                pivot_v = v
        if pivot_row > pivot:
            for i in range(M):
                A[pivot][i], A[pivot_row][i] = A[pivot_row][i], A[pivot][i]
        if pivot_v == 0:
            return ('False', A)
        inv_pivot = 1 / A[pivot][pivot]
        A[pivot][pivot] = 1
        for i in range(pivot + 1, M):
            A[pivot][i] *= inv_pivot
        for i in range(N):
            if i == pivot:
                continue
            t = -1 * A[i][pivot]
            A[i][pivot] = 0
            for j in range(pivot + 1, M):
                A[i][j] += t * A[pivot][j]
        pivot += 1
    return ('True', A)

n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    a = list(perpendicular_bisector((x1, y1), (x2, y2)))
    b = list(perpendicular_bisector((x1, y1), (x3, y3)))
    c = [a, b]
    state, c = gauss_jordan_elimination(c)
    x = -c[0][2]
    y = -c[1][2]
    r = ((x - x1)**2 + (y - y1)**2)**0.5
    print('{0:.3f} {1:.3f} {2:.3f}'.format(round(x, 3), round(y, 3), round(r, 3)))

Solution (Python):
from math import sqrt

def circle(x1, y1, x2, y2, x3, y3):
    if x1 == 0:
        dx = 1
        x1 = x1 + dx
        x2 = x2 + dx
        x3 = x3 + dx
    else:
        dx = 0
    if y2 == 0:
        dy = 1
        y1 = y1 + dy
        y2 = y2 + dy
        y3 = y3 + dy
    else:
        dy = 0
    A = [[x1, y1, 1, 1, 0, 0],[x2, y2, 1, 0, 1, 0],[x3, y3, 1, 0, 0, 1]]

    for i in range(3):
        A[0] = [x/A[0][0] for x in A[0]]
        A[1] = [A[1][j] - A[1][0] * A[0][j] for j in range(6)]
        A[2] = [A[2][j] - A[2][0] * A[0][j] for j in range(6)]

        for j in range(3):
            A[j] = A[j][1:] + A[j][:1]
        A = A[1:] + A[:1]

    for i in range(3):
        A[i] = A[i][:3]

    V = [-x1**2-y1**2, -x2**2-y2**2, -x3**2-y3**2]
    M = [(A[i][0] * V[0] + A[i][1] * V[1] + A[i][2] * V[2]) for i in range(3)]
    xcenter = -0.5 * M[0] - dx
    ycenter = -0.5 * M[1] - dy
    radius = sqrt((M[0]**2) /4 + (M[1]**2) /4 - M[2])
    return xcenter, ycenter, radius

n = int(input())
for line in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    xc, yc, ra = circle(x1, y1, x2, y2, x3, y3)
    print('%.3f %.3f %.3f' % (xc, yc, ra))

Solution (Python):
import math

n = int(raw_input())

for i in range(n):

  x1, y1, x2, y2, x3, y3 = map(float, raw_input().split())

  px = ((y2 - y3)*(x1*x1 + y1*y1) + (y3 - y1)*(x2*x2 + y2*y2) + (y1 - y2)*(x3*x3 + y3*y3))/(2*(x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2)))
  py = ((x2 - x3)*(x1*x1 + y1*y1) + (x3 - x1)*(x2*x2 + y2*y2) + (x1 - x2)*(x3*x3 + y3*y3))/(2*(y1*(x2 - x3) + y2*(x3 - x1) + y3*(x1 - x2)))

  r = math.sqrt(pow((x1 - px), 2) + pow((y1 - py), 2))

  print "%.3f %.3f %.3f" % (px, py, r)

Solution (Python):
import math

def solve(a,b,c,d,e,f):
	x = - (d*(f**2+e**2-b**2-a**2) + b*(-f**2-e**2) + b**2*f + a**2*f + d**2*(b-f) + c**2*(b-f)) / (c*(2*f-2*b) - 2*a*f + 2*b*e + d*(2*a-2*e))
	y = (c*(f**2+e**2-b**2-a**2) + a*(-f**2-e**2) + b**2*e + a**2*e + d**2*(a-e) + c**2*(a-e)) / (c*(2*f-2*b) - 2*a*f + 2*b*e + d*(2*a - 2*e))
	r = math.hypot(x-a, y-b) 
	return x,y,r

n = int(input())
for _ in range(n):
	a,b,c,d,e,f = map(float, input().split())
	x,y,r = solve(a,b,c,d,e,f)
	print("{:.3f} {:.3f} {:.3f}".format(x, y, r))

Solution (Python):
import math

n = int(input())
for i in range(n):
    x1,y1,x2,y2,x3,y3 = map(float,input().split())
    a1,b1,c1 = 2*(x2-x1),2*(y2-y1), (x1**2 - x2 **2 + y1 ** 2 - y2 ** 2)
    a2,b2,c2 = 2*(x3-x1), 2*(y3-y1), (x1**2 - x3 **2 + y1 ** 2 - y3 ** 2)

    p1 = (b1*c2 - b2*c1)/(a1*b2 - a2*b1)
    p2 = (c1*a2 - c2*a1)/(a1*b2 - a2*b1)
    r = math.hypot(x1-p1, y1-p2)
    temp = [p1,p2,r]
    ans = map(lambda x:round(x,3),temp)
    print("%03.3f %03.3f %03.3f"%(temp[0],temp[1],temp[2]))

Solution (Python):
import math

def Cramer(c):
	den = c[0]*c[4] - c[1]*c[3]
	return [(c[2]*c[4] - c[1]*c[5]) / den, (c[0]*c[5] - c[2]*c[3]) / den]

n = eval(input())
for i in range(n):
	c = [eval(item) for item in input().split()]
	sol = Cramer([2*(c[0] - c[2]),\
				  2*(c[1] - c[3]),\
				  c[0]**2 + c[1]**2 - c[2]**2 - c[3]**2,\
				  2*(c[0] - c[4]),\
				  2*(c[1] - c[5]),\
				  c[0]**2 + c[1]**2 - c[4]**2 - c[5]**2])
	r = math.sqrt((sol[0] - c[0])**2+(sol[1] - c[1])**2)
	print('%.3f %.3f %.3f' %(sol[0], sol[1], r))

Solution (Python):
for _ in range(int(input())):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    d = 2 * (x2 * y3 - x3 * y2 + x3 * y1 - x1 * y3 + x1 * y2 - x2 * y1)
    px = ((y2 - y3) * (x1 ** 2 + y1 ** 2) + (y3 - y1) * (x2 ** 2 + y2 ** 2) + (y1 - y2) * (x3 ** 2 + y3 ** 2)) / d
    py = -1 * ((x2 - x3) * (x1 ** 2 + y1 ** 2) + (x3 - x1) * (x2 ** 2 + y2 ** 2) + (x1 - x2) * (x3 ** 2 + y3 ** 2)) / d
    print('{0:.3f} {1:.3f} {2:.3f}'.format(px, py, ((x1 - px) ** 2 + (y1 - py) ** 2) ** 0.5))

Solution (Python):
for _ in[0]*int(input()):
 a,d,b,e,c,f=map(float,input().split())
 z=2*(b*f-c*e+c*d-a*f+a*e-b*d)
 x=((e-f)*(a**2+d**2)+(f-d)*(b**2+e**2)+(d-e)*(c**2+f**2))/z
 y=((c-b)*(a**2+d**2)+(a-c)*(b**2+e**2)+(b-a)*(c**2+f**2))/z
 print('{0:.3f} {1:.3f} {2:.3f}'.format(x,y,((a-x)**2+(d-y)**2)**0.5))

Solution (Python):
import math

def simultaneous_equasion(a, b, c, d, e, f):
    "??£???????¨????"
    det = a * d - b * c
    a11 = d / det
    a12 = - b / det
    a21 = - c / det
    a22 = a / det
    return a11 * e + a12 * f, a21 * e + a22 * f

n = int(input())
for i in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    
    
    
    
    a = 2 * (x1 - x2)
    b = 2 * (y1 - y2)
    c = 2 * (x1 - x3)
    d = 2 * (y1 - y3)
    e = x1 ** 2 + y1 ** 2 - x2 ** 2 - y2 ** 2
    f = x1 ** 2 + y1 ** 2 - x3 ** 2 - y3 ** 2
    px, py = simultaneous_equasion(a, b, c, d, e, f)
    r = math.sqrt((px - x1) ** 2 + (py - y1) ** 2)
    print("%.3f %.3f %.3f" % (round(px, 3), round(py, 3), round(r, 3)))

Solution (Python):


import sys

def length(a, b):
    return ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5

def solve_sim_equ(a, b, c, d, e, f):
    '''
    From Problem 0004.
    This function solves following equation.
    ax + by = c
    dx + ey = f
    '''
    if a==0 and d==0:
        if b==0 and e==0:
            return 0., 0.
        if b != 0:
            return 0., c/b+0.
        else:
            return 0., f/e+0.
    elif b==0 and e==0:
        if a != 0:
            return 0., d/a+0.
        else:
            return 0., a/d+0.

    if b == 0:
        a, d = d, a
        b, e = e, b
        c, f = f, c
    g = e / b
    x = (g*c - f) / (g*a - d)
    y = (c - a*x) / b
    return x+0., y+0.


def circumscribed_circle(x, y, z):
    def get_equ_coef(p, q):
        h_x = (p[0] + q[0]) / 2
        h_y = (p[1] + q[1]) / 2
        a = q[1] - p[1]
        b = p[0] - q[0]
        c = b * h_x - a * h_y
        return b, -a, c
    coef = get_equ_coef(x, y) + get_equ_coef(y, z)
    center = solve_sim_equ(*coef)
    r = length(center, x)
    return center, r


def main():
    N = int(input())
    for i in range(N):
        vs = [float(v) for v in input().split()]
        a = (vs[0], vs[1])
        b = (vs[2], vs[3])
        c = (vs[4], vs[5])
        center, r = circumscribed_circle(a, b, c)
        print('{0:.3f} {1:.3f} {2:.3f}'.format(center[0], center[1], r))


if __name__ == '__main__':
    main()

Solution (Python):
import math

n=int(input())
for i in range(n):
    deta=[float(i) for i in input().split()]
    h1=math.sqrt((deta[2]-deta[0])**2+(deta[3]-deta[1])**2)
    h2=math.sqrt((deta[4]-deta[2])**2+(deta[5]-deta[3])**2)
    h3=math.sqrt((deta[0]-deta[4])**2+(deta[1]-deta[5])**2)
    sub=(h1+h2+h3)/2
    s=math.sqrt(sub*(sub-h1)*(sub-h2)*(sub-h3))
    sum=((h1*h2*h3)/s)/4
    a=2*deta[2]-2*deta[0]
    b=2*deta[3]-2*deta[1]
    c=-(deta[0]**2+deta[1]**2-(deta[2]**2)-(deta[3]**2))
    d=2*deta[4]-2*deta[2]
    e=2*deta[5]-2*deta[3]
    f=-(deta[2]**2+deta[3]**2-(deta[4]**2)-(deta[5]**2))
    
    x=(e*c-b*f)/(a*e-b*d)
    y=(a*f-c*d)/(a*e-b*d)
    print("{0:.3f} {1:.3f} {2:.3f}".format(x,y,sum))

Solution (Python):
import math

n = input()
for i in xrange(n):
    x1, y1, x2, y2, x3, y3 = map(float, raw_input().split())
    p = ((y1-y3)*(y1**2 - y2**2 + x1**2 - x2**2) - \
         (y1-y2)*(y1**2 - y3**2 + x1**2 - x3**2)) / \
         (2*(y1-y3)*(x1-x2)-2*(y1-y2)*(x1-x3))

    q = ((x1-x3)*(x1**2 - x2**2 + y1**2 - y2**2) - \
         (x1-x2)*(x1**2 - x3**2 + y1**2 - y3**2)) / \
         (2*(x1-x3)*(y1-y2)-2*(x1-x2)*(y1-y3))

    print '%.3f %.3f %.3f'%(round(p,3), round(q,3), round(math.sqrt((x1-p)**2+(y1-q)**2), 3))

Solution (Python):
from math import sqrt
n = int(input())
for i in range(n):
    x1,y1,x2,y2,x3,y3 = list(map(float, input().split()))
    c = 2*((x2 - x1)*(y3 - y1) - (y2 - y1)*(x3 - x1))
    ox = ((y3 - y1)*(x2**2 - x1**2 + y2**2 - y1**2) + (y1 - y2)*(x3**2 - x1**2 + y3**2 - y1**2))/c
    oy = ((x1 - x3)*(x2**2 - x1**2 + y2**2 - y1**2) + (x2 - x1)*(x3**2 - x1**2 + y3**2 - y1**2))/c
    r = sqrt((x1-ox)**2+(y1-oy)**2)
    print("{0:.3f} {1:.3f} {2:.3f}".format(ox+0.0, oy+0.0, r+0.0))

Solution (Python):
import math
n = int(input())
for i in range(n):
    x1,y1,x2,y2,x3,y3 = map(float,input().split())
    a = 2*(x2 - x1)
    b = 2*(y2 - y1)
    c = x2**2 + y2**2 - x1**2 -y1**2
    d = 2*(x3 - x1)
    e = 2*(y3 - y1)
    f = x3**2 + y3**2 - x1**2 -y1**2
    x = (c*e - f*b)/(a*e - b*d)
    y = (c*d - f*a)/(b*d - e*a)
    r = math.sqrt((x1 - x)**2 + (y1 - y)**2)
    x = round(x,3)
    y = round(y,3)
    r = round(r,3)
    if x == -0.0:
        x = 0.0
    if y == -0.0:
        y = 0.0
    print("{0:.3f}".format(x),end = ' ')
    print("{0:.3f}".format(y),end = ' ')
    print("{0:.3f}".format(r))

Solution (Python):
from math import cos, sin, sqrt, radians, degrees, acos, fabs

if __name__ == '__main__':
    epsilon = 1e-9

    
    num = int(input())
    for i in range(num):
        x1, y1, x2, y2, x3, y3 = [float(x) for x in input().split(' ')]

        
        a = sqrt((x1-x2)**2 + (y1-y2)**2)
        b = sqrt((x2-x3)**2 + (y2-y3)**2)
        c = sqrt((x1-x3)**2 + (y1-y3)**2)
        cosA = (b**2+c**2-a**2)/(2*b*c)

        
        r = a / sin(acos(cosA)) / 2

        """
       ??????O?????§?¨????(x, y)??¨????????¨
       (x-x1)**2 + (y-y1)**2 = (x-x2)**2 + (y-y2)**2
       x**2 -2*x1*x + x1**2 + y**2 -2*y1*y + y1**2 = ... -2*x2*x, x2**2, -2*y2*y, y2**2
       ?????¨????????¨ (-2*x1 + 2*x2)x + (-2y1 + 2*y2)y + (x1**2 + y1**2 - x2**2 - y2**2) = 0
       x1, x3???????????????????§????
       (-2*x1 + 2*x3)x + (-2y1 + 2*y3)y + (x1**2 + y1**2 - x3**2 - y3**2) = 0
       """
        A = -2*x1 + 2*x2
        B = -2*y1 + 2*y2
        C = -2*x1 + 2*x3
        D = -2*y1 + 2*y3
        E = x1**2 + y1**2 - x2**2 - y2**2
        F = x1**2 + y1**2 - x3**2 - y3**2

        x = 1/(A*D-B*C) * (D*E - B*F)
        y = 1/(A*D-B*C) * (-C*E + A*F)
        
        if fabs(x) < epsilon:
            x = 0.0
        if fabs(y) < epsilon:
            y = 0.0

        print('{0:.3f} {1:.3f} {2:.3f}'.format(-x, -y, r))

Solution (Python):
from math import sqrt, fabs


def calcu_cirucumcenter(x1, y1, x2, y2, x3, y3):
    """
   ??????O?????§?¨????(x, y)??¨????????¨
   (x-x1)**2 + (y-y1)**2 = (x-x2)**2 + (y-y2)**2
   x**2 -2*x1*x + x1**2 + y**2 -2*y1*y + y1**2 = ... -2*x2*x, x2**2, -2*y2*y, y2**2
   ?????¨????????¨ (-2*x1 + 2*x2)x + (-2y1 + 2*y2)y + (x1**2 + y1**2 - x2**2 - y2**2) = 0
   x1, x3???????????????????§????
   (-2*x1 + 2*x3)x + (-2y1 + 2*y3)y + (x1**2 + y1**2 - x3**2 - y3**2) = 0
   
   ??????????????¢????????\????????¢???????????£???????¨??????¨???????§£???
   | a b | = |e|
   | c d |   |f|
   """
    a = -2 * x1 + 2 * x2
    b = -2 * y1 + 2 * y2
    c = -2 * x1 + 2 * x3
    d = -2 * y1 + 2 * y3
    e = -1* (x1 ** 2 + y1 ** 2 - x2 ** 2 - y2 ** 2)
    f = -1 * (x1 ** 2 + y1 ** 2 - x3 ** 2 - y3 ** 2)

    x = 1 / (a * d - b * c) * (d * e - b * f)
    y = 1 / (a * d - b * c) * (-c * e + a * f)
    return x, y


if __name__ == '__main__':
    epsilon = 1e-9

    
    num = int(input())
    for i in range(num):
        x1, y1, x2, y2, x3, y3 = [float(x) for x in input().split(' ')]

        
        x, y = calcu_cirucumcenter(x1, y1, x2, y2, x3, y3)
        
        if fabs(x) < epsilon:
            x = 0.0
        if fabs(y) < epsilon:
            y = 0.0

        
        r = sqrt((x - x1)**2 + (y - y1)**2)

        print('{0:.3f} {1:.3f} {2:.3f}'.format(x, y, r))

Solution (Python):
n = int(input())
datasets = [0]*n

for i in range(0,n):
  datasets[i] = list(map(float,input().split()))

for data in datasets:
  x1p2=(data[0]+data[2])
  x1m2=(data[0]-data[2])
  x1p3=(data[0]+data[4])
  x1m3=(data[0]-data[4])
  y1p2=(data[1]+data[3])
  y1m2=(data[1]-data[3])
  y1p3=(data[1]+data[5])
  y1m3=(data[1]-data[5])
  x=(x1p2*x1m2*y1m3+y1p2*y1m2*y1m3-x1p3*x1m3*y1m2-y1p3*y1m3*y1m2)/(2*(x1m2*y1m3-x1m3*y1m2))
  y=(y1p2*y1m2*x1m3+x1p2*x1m2*x1m3-y1p3*y1m3*x1m2-x1p3*x1m3*x1m2)/(2*(y1m2*x1m3-y1m3*x1m2))
  r=((x-data[0])**2+(y-data[1])**2)**(1/2)
  if x == 0.000:
    x=0
  if y == 0.000:
    y=0
  print("{0:.3f} {1:.3f} {2:.3f}".format(x,y,r))