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):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):
from decimal import Decimal
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=list(map(Decimal,input().split()))
    a=x1-x3
    b=y2-y3
    c=x2-x3
    d=y1-y3
    e=y1-y2
    f=x1+x3
    g=x2+x3
    X=(b*d*e+a*b*f-c*d*g)/2/(a*b-c*d)
    try:
        Y=-(a/d)*(X-f/2)+(y1+y3)/2
    except:
        Y=-(c/b)*(X-g/2)+(y2+y3)/2
    R=math.sqrt((x1-X)**2+(y1-Y)**2)
    print(" ".join(["{:.3f}".format(i) for i in [X,Y,R]]))

Solution (Python):



import math
EPS = 1e-8

def cross(a, b):
	return a.real*b.imag - a.imag*b.real
def dist(a, b):
	return math.hypot(a.real-b.real, a.imag-b.imag)
	

def crossPointLL(ln1, ln2):
	u = ln1[1]-ln1[0]
	v = ln2[1]-ln2[0]
	return ln1[0] + u*(cross(v, ln2[0]-ln1[0])/cross(v, u))


def circumscribed_circle(p1, p2, p3):
	p12 = (p1+p2)/2
	p23 = (p2+p3)/2
	ln1 = [p12, p12+(p1-p2)*complex(0, 1)]
	ln2 = [p23, p23+(p2-p3)*complex(0, 1)]
	c = crossPointLL(ln1, ln2)
	return [c, dist(c, p1)]

for i in range(int(input())):
	p = list(map(float, input().split()))
	p1 = complex(p[0], p[1])
	p2 = complex(p[2], p[3])
	p3 = complex(p[4], p[5])
	ans = circumscribed_circle(p1, p2, p3)
	print(format(ans[0].real+EPS, ".3f"), format(ans[0].imag+EPS, ".3f"), format(ans[1]+EPS, ".3f"))

Solution (Python):

import math

PI = math.pi
EPS = 10**-10

def edge(a, b):
    return ((a[0]-b[0])**2+(a[1]-b[1])**2)**.5

def area(a, b, c):
    s = (a+b+c)/2
    return (s*(s-a)*(s-b)*(s-c))**.5

def LawOfCosines(a, b, c): 
    return math.acos( (b*b+c*c-a*a) / (2.0*b*c) );

def is_same(x, y): 
    return abs(x-y) < EPS

class Triangle:
    def __init__(self, p):
        a, b, c = p
        self.a = a
        self.b = b
        self.c = c

        self.edgeA = edge(b, c)
        self.edgeB = edge(c, a)
        self.edgeC = edge(a, b)

        self.area = area(self.edgeA, self.edgeB, self.edgeC)

        self.angleA = LawOfCosines(self.edgeA, self.edgeB, self.edgeC)
        self.angleB = LawOfCosines(self.edgeB, self.edgeC, self.edgeA)
        self.angleC = LawOfCosines(self.edgeC, self.edgeA, self.edgeB)

    def circumscribeadCircleRadius(self): 
        return self.edgeA / math.sin(self.angleA) / 2.0

    def circumscribedCircleCenter(self): 
        a = math.sin(2.0*self.angleA);
        b = math.sin(2.0*self.angleB);
        c = math.sin(2.0*self.angleC);
        X = (self.a[0] * a + self.b[0] * b + self.c[0] * c) / (a+b+c);
        Y = (self.a[1] * a + self.b[1] * b + self.c[1] * c) / (a+b+c);
        return X, Y

    def inscribedCircleRadius(self): 
        return 2 * self.area / (self.edgeA + self.edgeB + self.edgeC)

    def inscribedCircleCenter(self): 
        points = [self.a, self.b, self.c]
        edges = [self.edgeA, self.edgeB, self.edgeC]
        s = sum(edges)
        return [sum([points[j][i]*edges[j] for j in range(3)])/s for i in range(2)]

    def isInner(self, p): 
        cross = lambda a, b: a[0]*b[1]-a[1]*b[0]

        c1 = 0
        c2 = 0
        points = [self.a, self.b, self.c]
        for i in range(3):
            a = [points[i][0]-points[(i+1)%3][0], points[i][1]-points[(i+1)%3][1]]
            b = [points[i][0]-p[0],  points[i][1]-p[1]]
            c = cross(a, b)
            if c > 0:
                c1 += 1
            elif c < 0:
                c2 += 1
        if c1 == 3 or c2 == 3:
            return True
        else:
            return c1+c2 != 3 and (c1 == 0 or c2 == 0)


if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        points = []
        c = list(map(float, input().split()))
        points = [(c[i], c[i+1]) for i in range(0, 6, 2)]
        t = Triangle(points)

        
        x, y = t.circumscribedCircleCenter()
        r = t.circumscribeadCircleRadius()
        print("{:.3f} {:.3f} {:.3f}".format(x,y,r))

Solution (Python):
import math

def calc(x, y):
    s = math.fabs((x[1]-x[0])*(y[2]-y[0]) - (x[2]-x[0])*(y[1]-y[0]))/2
    a = ((x[2]-x[1])**2 + (y[2]-y[1])**2)**0.5
    b = ((x[0]-x[2])**2 + (y[0]-y[2])**2)**0.5
    c = ((x[1]-x[0])**2 + (y[1]-y[0])**2)**0.5
    r = a*b*c/(4*s)

    a = [2*(x[1]-x[0]), 2*(x[2]-x[0])]
    b = [2*(y[1]-y[0]), 2*(y[2]-y[0])]
    c = [x[0]**2 - x[1]**2 + y[0]**2 -y[1]**2, x[0]**2 - x[2]**2 + y[0]**2 -y[2]**2]
    x = (b[0]*c[1]-b[1]*c[0])/(a[0]*b[1]-a[1]*b[0])
    y = (c[0]*a[1]-c[1]*a[0])/(a[0]*b[1]-a[1]*b[0])

    return [x,y,r]

for i in range(int(input())):
    list = [float(l) for l in raw_input().split()]
    x,y = [list[0::2],list[1::2]]

    ans = calc(x,y)

    print "%.3f %.3f %.3f" % (ans[0],ans[1],ans[2])

Solution (Python):
import sys

n=input()
for line in sys.stdin:
    x=map(float,line.split())[0::2];
    y=map(float,line.split())[1::2];
    
    A=x[0]-x[1]
    B=y[0]-y[1]
    C=x[1]-x[2]
    D=y[1]-y[2]
    E=-(x[0]**2+y[0]**2)+(x[1]**2+y[1]**2)
    F=-(x[1]**2+y[1]**2)+(x[2]**2+y[2]**2)
    l=(D*E-B*F)/(A*D-B*C)
    m=(-C*E+A*F)/(A*D-B*C)
    n=-(x[0]**2+y[0]**2+l*x[0]+m*y[0])

    print "{:.3f} {:.3f} {:.3f}".format(-l/2,-m/2,(l**2+m**2-4*n)**0.5/2)

Solution (Python):

def solve(a, b, c, d, e, f):
    agn = a*e - b*d
    x = (e*c - b*f)/float(agn)
    y = (-d*c + a*f)/float(agn)
    if x == 0.: x = 0.
    if y == 0.: y = 0.
    return (x, y)
   
while 1:
    try:
        n = input()
        for i in xrange(n):
            x = [0 for i in xrange(3)]
            y = [0 for i in xrange(3)]
            a,b,t = [[0 for i in xrange(2)] for i in xrange(3)]
            p,q,s = [[0 for i in xrange(2)] for i in xrange(3)]
            x[0],y[0],x[1],y[1],x[2],y[2] = map(float, raw_input().split())
            for j in xrange(2):
                a[j] = (x[j+1] + x[j])/2
                b[j] = (y[j+1] + y[j])/2
                if x[j+1] - x[j] == 0.:
                    p[j] = 0.
                    q[j] = -1.
                    s[j] = -b[j]
                elif y[j+1] - y[j] == 0.:
                    p[j] = -1.
                    q[j] = 0.
                    s[j] = -a[j]
                else:
                    t[j] = (y[j+1] - y[j]) / (x[j+1] - x[j])
                    p[j] = -1./t[j]
                    q[j] = -1.
                    s[j] = -(a[j]/t[j])-b[j]
            c = solve(p[0], q[0], s[0], p[1], q[1], s[1])
            r = ((x[0]-c[0])**2 + (y[0]-c[1])**2)**0.5
            print "%.3f %.3f %.3f" % (c[0], c[1], r)
    except EOFError:
        break

Solution (Python):
def heron(a,b,c):
    s = 0.5*(a+b+c)
    return (s*(s-a)*(s-b)*(s-c))**0.5

def gaishin(a,b,c,z1,z2,z3,s):
    p = (a**2*(b**2 + c**2 - a**2)*z1 + \
         b**2*(c**2 + a**2 - b**2)*z2 + \
         c**2*(a**2 + b**2 - c**2)*z3)/(16*s**2)
    return p

n = input()

for i in range(n):
    x1,y1,x2,y2,x3,y3 = map(float, raw_input().split(' '))
    l1 = ((x2-x3)**2+(y2-y3)**2)**0.5
    l2 = ((x3-x1)**2+(y3-y1)**2)**0.5
    l3 = ((x1-x2)**2+(y1-y2)**2)**0.5
    s = heron(l1,l2,l3)
    px = gaishin(l1,l2,l3,x1,x2,x3,s)
    py = gaishin(l1,l2,l3,y1,y2,y3,s)
    r = ((px-x1)**2+(py-y1)**2)**0.5
    print '%.3f %.3f %.3f' % (px,py,r)

Solution (Python):
import math
from math import *
for i in range(int(raw_input())):
    (x1, y1, x2, y2, x3, y3) = map(float, raw_input().split())
    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
    xp = (b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1)
    yp = (c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1)
    r = sqrt((x1 - xp) ** 2 + (y1 - yp) ** 2)
    print '%.3f %.3f %.3f' % (xp, yp, r)

Solution (Python):
import math
 
def calc(x, y):
    s = math.fabs((x[1]-x[0])*(y[2]-y[0]) - (x[2]-x[0])*(y[1]-y[0]))/2
    a = ((x[2]-x[1])**2 + (y[2]-y[1])**2)**0.5
    b = ((x[0]-x[2])**2 + (y[0]-y[2])**2)**0.5
    c = ((x[1]-x[0])**2 + (y[1]-y[0])**2)**0.5
    r = a*b*c/(4*s)
 
    a = [2*(x[1]-x[0]), 2*(x[2]-x[0])]
    b = [2*(y[1]-y[0]), 2*(y[2]-y[0])]
    c = [x[0]**2 - x[1]**2 + y[0]**2 -y[1]**2, x[0]**2 - x[2]**2 + y[0]**2 -y[2]**2]
    x = (b[0]*c[1]-b[1]*c[0])/(a[0]*b[1]-a[1]*b[0])
    y = (c[0]*a[1]-c[1]*a[0])/(a[0]*b[1]-a[1]*b[0])
 
    return [x,y,r]
 
for i in range(int(input())):
    list = [float(l) for l in raw_input().split()]
    x,y = [list[0::2],list[1::2]]
 
    ans = calc(x,y)
 
    print "%.3f %.3f %.3f" % (ans[0],ans[1],ans[2])

Solution (Python):
import math
 
def calc(x, y):
    s = math.fabs((x[1]-x[0])*(y[2]-y[0]) - (x[2]-x[0])*(y[1]-y[0]))/2
    a = ((x[2]-x[1])**2 + (y[2]-y[1])**2)**0.5
    b = ((x[0]-x[2])**2 + (y[0]-y[2])**2)**0.5
    c = ((x[1]-x[0])**2 + (y[1]-y[0])**2)**0.5
    r = a*b*c/(4*s)
 
    a = [2*(x[1]-x[0]), 2*(x[2]-x[0])]
    b = [2*(y[1]-y[0]), 2*(y[2]-y[0])]
    c = [x[0]**2 - x[1]**2 + y[0]**2 -y[1]**2, x[0]**2 - x[2]**2 + y[0]**2 -y[2]**2]
    x = (b[0]*c[1]-b[1]*c[0])/(a[0]*b[1]-a[1]*b[0])
    y = (c[0]*a[1]-c[1]*a[0])/(a[0]*b[1]-a[1]*b[0])
 
    return [x,y,r]
 
for i in range(int(input())):
    list = [float(l) for l in raw_input().split()]
    x,y = [list[0::2],list[1::2]]
 
    ans = calc(x,y)
 
    print "%.3f %.3f %.3f" % (ans[0],ans[1],ans[2])

Solution (Python):
import math
 
def calc(x, y):
    s = math.fabs((x[1]-x[0])*(y[2]-y[0]) - (x[2]-x[0])*(y[1]-y[0]))/2
    a = ((x[2]-x[1])**2 + (y[2]-y[1])**2)**0.5
    b = ((x[0]-x[2])**2 + (y[0]-y[2])**2)**0.5
    c = ((x[1]-x[0])**2 + (y[1]-y[0])**2)**0.5
    r = a*b*c/(4*s)
 
    a = [2*(x[1]-x[0]), 2*(x[2]-x[0])]
    b = [2*(y[1]-y[0]), 2*(y[2]-y[0])]
    c = [x[0]**2 - x[1]**2 + y[0]**2 -y[1]**2, x[0]**2 - x[2]**2 + y[0]**2 -y[2]**2]
    x = (b[0]*c[1]-b[1]*c[0])/(a[0]*b[1]-a[1]*b[0])
    y = (c[0]*a[1]-c[1]*a[0])/(a[0]*b[1]-a[1]*b[0])
 
    return [x,y,r]
 
for i in range(int(input())):
    list = [float(l) for l in raw_input().split()]
    x,y = [list[0::2],list[1::2]]
 
    ans = calc(x,y)
 
    print "%.3f %.3f %.3f" % (ans[0],ans[1],ans[2])

Solution (Python):
import math
 
def calc(x, y):
    s = math.fabs((x[1]-x[0])*(y[2]-y[0]) - (x[2]-x[0])*(y[1]-y[0]))/2
    a = ((x[2]-x[1])**2 + (y[2]-y[1])**2)**0.5
    b = ((x[0]-x[2])**2 + (y[0]-y[2])**2)**0.5
    c = ((x[1]-x[0])**2 + (y[1]-y[0])**2)**0.5
    r = a*b*c/(4*s)
 
    a = [2*(x[1]-x[0]), 2*(x[2]-x[0])]
    b = [2*(y[1]-y[0]), 2*(y[2]-y[0])]
    c = [x[0]**2 - x[1]**2 + y[0]**2 -y[1]**2, x[0]**2 - x[2]**2 + y[0]**2 -y[2]**2]
    x = (b[0]*c[1]-b[1]*c[0])/(a[0]*b[1]-a[1]*b[0])
    y = (c[0]*a[1]-c[1]*a[0])/(a[0]*b[1]-a[1]*b[0])
 
    return [x,y,r]
 
for i in range(int(input())):
    list = [float(l) for l in raw_input().split()]
    x,y = [list[0::2],list[1::2]]
 
    ans = calc(x,y)
 
    print "%.3f %.3f %.3f" % (ans[0],ans[1],ans[2])

Solution (Python):
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from math import sqrt
N = int(stdin.readline())
for i in xrange(N):
    x1, y1, x2, y2, x3, y3 = (float(n) for n in stdin.readline().split())
    det = x1*y2 + x2*y3 + x3*y1 - x1*y3 - x2*y1 - x3*y2
    a = x1**2 + y1**2
    b = x2**2 + y2**2
    c = x3**2 + y3**2
    L = (a*y2 + b*y3 + c*y1 - a*y3 - b*y1 - c*y2) / det
    M = (x1*b + x2*c + x3*a - x1*c - x2*a - x3*b) / det
    N = (x1*y2*c + x2*y3*a + x3*y1*b - x1*y3*b - x2*y1*c - x3*y2*a) / det
    xp = L / 2.0
    yp = M / 2.0
    r = sqrt(abs(N + xp**2.0 + yp**2.0))
    print('{:.3f} {:.3f} {:.3f}'.format(xp, yp, r))

Solution (Python):
from math import sqrt 
ans=[]
n=input()
for i in xrange(n):
    x1,y1,x2,y2,x3,y3=map(float,raw_input().split())
    A1=2*(x2-x1)
    A2=2*(x3-x1)
    B1=2*(y2-y1)
    B2=2*(y3-y1)
    C1=x1**2-x2**2+y1**2-y2**2
    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=sqrt((x-x1)**2 + (y-y1)**2)
    ansstr='%.3f %.3f %.3f' % (x,y,r)
    ans.append(ansstr)
for i in ans:
    print i

Solution (Python):
import sys
from math import sqrt
n = input()
for line in sys.stdin:
    [x1,y1,x2,y2,x3,y3] = [float(x) for x in line.split()]
    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)
    a = sqrt((x2-x1)**2 + (y2-y1)**2)
    b = sqrt((x3-x1)**2 + (y3-y1)**2)
    c = sqrt((x3-x2)**2 + (y3-y2)**2)
    r = (a*b*c)/sqrt((a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c))
    print "%.3f %.3f %.3f" % (x,y,r)

Solution (Python):
import math
  
a = int(raw_input())
  
while a > 0:
    a -= 1
    x1,y1,x2,y2,x3,y3 = map(float,raw_input().split())
  
    a1 = 2*(x2-x1)
    b1 = 2*(y2-y1)
    c1 = x1*x1-x2*x2+y1*y1-y2*y2
    a2 = 2*(x3-x1)
    b2 = 2*(y3-y1)
    c2 = x1*x1-x3*x3+y1*y1-y3*y3
  
    X = (b1*c2-b2*c1)/(a1*b2-a2*b1)
    Y = (c1*a2-c2*a1)/(a1*b2-a2*b1)
  
    R = math.sqrt((X-x1)*(X-x1)+(Y-y1)*(Y-y1))                                     
  
    print "%.3f %.3f %.3f" % (X,Y,R)

Solution (Python):
import math

a = int(raw_input())

while a	> 0:
    a -= 1
    x1,y1,x2,y2,x3,y3 =	map(float,raw_input().split())

    a1 = 2*(x2-x1)
    b1 = 2*(y2-y1)
    c1 = x1*x1-x2*x2+y1*y1-y2*y2
    a2 = 2*(x3-x1)
    b2 = 2*(y3-y1)
    c2 = x1*x1-x3*x3+y1*y1-y3*y3

    X = (b1*c2-b2*c1)/(a1*b2-a2*b1)
    Y = (c1*a2-c2*a1)/(a1*b2-a2*b1)

    R = math.sqrt((X-x1)*(X-x1)+(Y-y1)*(Y-y1))

    print "%.3f %.3f %.3f" % (X,Y,R)

Solution (Python):
import math

def equation(A):
    n = len(A)
    for i in range(n):
        j=i
        while A[j][i]==0:
            j+= 1
        A[i], A[j] = A[j], A[i]
        A[i] = [e / A[i][i] for e in A[i]]
        for j in range(n):
            if j==i: continue
            tmp = A[j][i]
            for k in range(n+1):
                A[j][k] -= tmp * A[i][k]
    return

n=input()
for i in range(n):
    A=[]
    seq = map(float, raw_input().split())
    for j in range(3):
        x = seq.pop(0)
        y = seq.pop(0)
        A.append([x, y, 1, -(x**2+y**2)])
    equation(A)

    x0, y0 = -A[0][3]/2, -A[1][3]/2
    r =((x-x0)**2 + (y-y0)**2)**.5
    print "%.3f %.3f %.3f" %(x0, y0, r)

Solution (Python):
import math

n = int(raw_input())

for _ in range(n):
    (x1, y1, x2, y2, x3, y3) = map(float, raw_input().split())

    a1 = (x2 - x1) * 2
    a2 = (x3 - x1) * 2
    b1 = (y2 - y1) * 2
    b2 = (y3 - y1) * 2
    c1 = x1**2 - x2**2 + y1**2 - y2**2
    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 = math.sqrt((x-x1)**2 + (y-y1)**2)

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

Solution (Python):


import sys
import math


n = 0
datas = []

for (i, line) in enumerate(sys.stdin):
	if i == 0:
		n = int(line)
	else:
		datas.append(map(float, line.split()))

for data in datas:
	x1, y1, x2, y2, x3, y3 = data
	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 sys
import math


n = 0
datas = []

for (i, line) in enumerate(sys.stdin):
	if i == 0:
		n = int(line)
	else:
		datas.append(map(float, line.split()))

for data in datas:
	x1, y1, x2, y2, x3, y3 = data
	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
n = input()
for i in range(n):
  x1, y1, x2, y2, x3, y3 = map(float, raw_input().split())
  d1 = x1 * x1 + y1 * y1;
  d2 = x2 * x2 + y2 * y2;
  d3 = x3 * x3 + y3 * y3;
  u = 0.5 / ( x1 * y2 - x2 * y1 + x2 * y3 - x3 * y2 + x3 * y1 - x1 * y3)
  xp = u* (d1 * y2 - d2 * y1 + d2 * y3 - d3 * y2 + d3 * y1 - d1 * y3)
  yp = u * (x1 * d2 - x2 * d1 + x2 * d3 - x3 * d2 + x3 * d1 - x1 * d3)
  r = math.sqrt((xp - x1) * (xp - x1) + (yp - y1) * (yp - y1))
  print "%.3f %.3f %.3f" % (xp, yp, r)

Solution (Python):
import math
def equation(A):
  n=len(A)
  for i in range(n):
    j=i
    while A[j][i]==0:j+=1
    A[i],A[j]= A[j],A[i]
    A[i]=[e/A[i][i] for e in A[i]]
    for j in range(n):
      if j==i: continue
      tmp=A[j][i]
      for k in range(n+1):A[j][k]-=tmp*A[i][k]
  return
n=input()
for i in range(n):
  A=[]
  seq=map(float,raw_input().split())
  for j in range(0,6,2):
    x,y=seq[j:j+2]
    A+=[[x,y,1,-(x**2+y**2)]]
  equation(A)
  x0=-A[0][3]/2
  y0=-A[1][3]/2
  r=((x-x0)**2+(y-y0)**2)**.5
  print "%.3f %.3f %.3f" %(x0,y0,r)

Solution (Python):


import sys

def prod_mat_vec(A, vec):
    ret = [0 for i in xrange(3)]
    for i in xrange(3):
        for j in xrange(3):
            ret[i] += A[i][j] * vec[j]
    return ret


def changerow(A, i1, i2):
    for j in xrange(3):
        buf = A[i1][j]
        A[i1][j] = A[i2][j]
        A[i2][j] = buf
    return A


def MatrixInverse3x3(A_src):
    A = [ [A_src[i][j] for j in xrange(3)] for i in xrange(3) ] 
    B = [ [1, 0, 0], \
          [0, 1, 0], \
          [0, 0, 1]  ]
    
    for i in xrange(3):
        if A[i][i] == 0:
            for k in xrange(i+1, 3):
                if A[k][k] != 0:
                    A = changerow(A, i, k)
                    B = changerow(B, i, k)
        a = A[i][i]

        for j in xrange(3):
            A[i][j] /= a
            B[i][j] /= a

        for k in xrange(3):
            if i == k: continue
            a = A[k][i]
            for j in xrange(3):
                A[k][j] -= A[i][j] * a
                B[k][j] -= B[i][j] * a
    return B


def output3x3(A):
    for i in xrange(3):
        print "%7.3f %7.3f %7.3f" % (A[i][0], A[i][1], A[i][2])
    return



for line in sys.stdin.readlines():
    List = map(float, line.strip().split())
    if len(List) == 1: continue
    [x1, y1, x2, y2, x3, y3] = List

    A = [ [x1, y1, 1.0] ,\
          [x2, y2, 1.0] ,\
          [x3, y3, 1.0]  ]
    vec = [x1**2+y1**2, x2**2+y2**2, x3**2+y3**2]

    B = MatrixInverse3x3(A)
    lmn = prod_mat_vec(B, vec)

    ans = [0.5*lmn[0], 0.5*lmn[1], (lmn[2]+0.25*lmn[0]**2+0.25*lmn[1]**2)**0.5]
    print "%.3f %.3f %.3f" % (ans[0], ans[1], ans[2])

Solution (Python):
import sys,math
if sys.version_info[0]>=3: raw_input=input
N=int(raw_input())
for i in range(N):
	x1,y1,x2,y2,x3,y3=[float(e) for e in raw_input().split()]
	a1=2*x2-2*x1
	b1=2*y2-2*y1
	c1=x1*x1-x2*x2+y1*y1-y2*y2
	a2=2*x3-2*x1
	b2=2*y3-2*y1
	c2=x1*x1-x3*x3+y1*y1-y3*y3
	x=(b1*c2-b2*c1)/(a1*b2-a2*b1)
	y=(c1*a2-c2*a1)/(a1*b2-a2*b1)
	print('%.3f %.3f %.3f'%(x,y,math.hypot(x1-x,y1-y)))

Solution (Python):
import math
n = int(raw_input())
while True:
    try:
        x1, y1, x2, y2, x3, y3 = map(float, raw_input().split())
        a = math.sqrt((x1-x2)**2+(y1-y2)**2)
        b = math.sqrt((x1-x3)**2+(y1-y3)**2)
        c = math.sqrt((x2-x3)**2+(y2-y3)**2)
        s = (a+b+c)/2
        ss = math.sqrt(s*(s-a)*(s-b)*(s-c))
        sina = 2*ss/b/c
        sinb = 2*ss/a/c
        sinc = 2*ss/a/b
     
        r = a/sina/2
    
        a = a*a
        b = b*b
        c = 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)
    except:
        break

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

    a = a*a
    b = b*b
    c = 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 sys
import math

n = int(raw_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):
from math import hypot

T = input()
for t in xrange(1, T + 1):
    x1, y1, x2, y2, x3, y3 = map(float, raw_input().split())
    x4 = (((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)))
    y4 = (((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" % (x4, y4, hypot(x1 - x4, y1 - y4))

Solution (Python):

import sys,math
if sys.version_info[0]>=3: raw_input=input
N=int(raw_input())
for i in range(N):
	x1,y1,x2,y2,x3,y3=[float(e) for e in raw_input().split()]
	a1=2*x2-2*x1
	b1=2*y2-2*y1
	c1=x1*x1-x2*x2+y1*y1-y2*y2
	a2=2*x3-2*x1
	b2=2*y3-2*y1
	c2=x1*x1-x3*x3+y1*y1-y3*y3
	x=(b1*c2-b2*c1)/(a1*b2-a2*b1)
	y=(c1*a2-c2*a1)/(a1*b2-a2*b1)
	print('%.3f %.3f %.3f'%(x,y,math.hypot(x1-x,y1-y)))

Solution (Python):
import sys,math
if sys.version_info[0]>=3: raw_input=input
N=int(raw_input())
for i in range(N):
	x1,y1,x2,y2,x3,y3=[float(e) for e in raw_input().split()]
	a1=2*x2-2*x1
	b1=2*y2-2*y1
	c1=x1*x1-x2*x2+y1*y1-y2*y2
	a2=2*x3-2*x1
	b2=2*y3-2*y1
	c2=x1*x1-x3*x3+y1*y1-y3*y3
	x=(b1*c2-b2*c1)/(a1*b2-a2*b1)
	y=(c1*a2-c2*a1)/(a1*b2-a2*b1)
	print('%.3f %.3f %.3f'%(x,y,math.hypot(x1-x,y1-y)))

Solution (Python):
from math import hypot

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

    d = lambda x, y: x*x + y*y
    t21 = [2*(x2-x1), 2*(y2-y1), d(x2,y2) - d(x1,y1)]
    t31 = [2*(x3-x1), 2*(y3-y1), d(x3,y3) - d(x1,y1)]

    det = lambda i, j: t21[i]*t31[j] - t21[j]*t31[i]
    a = det(2,1) / det(0,1)
    b = det(0,2) / det(0,1)
    r = hypot(x1-a, y1-b)

    print('{:.3f} {:.3f} {:.3f}'.format(a, b, r))

Solution (Python):
def f1(a, b, c, d):  
    k = (a - c) / (d - b)
    h = ((b + d) - k * (a + c)) / 2
    return k, h


def f2(a, b, c, d):  
    x = (d - b) / (a - c)
    y = a * x + b

    return x, y


def f3(a, b, c, d):
    return ((c - a) ** 2 + (d - b) ** 2) ** 0.5


def main():
    N = int(input())
    for i in range(N):
        a, b, c, d, e, f = map(float, input().split())
        T = []
        if not b == d:
            k, h = f1(a, b, c, d)
            T.append((k, h))
        if not d == f:
            k, h = f1(c, d, e, f)
            T.append((k, h))
        if not  f == b:
            k, h = f1(e, f, a, b)
            T.append((k, h))
        x, y = f2(T[0][0], T[0][1], T[1][0], T[1][1])
        r = f3(x, y, a, b)
        print(f'{round(x, 3):.3f} {round(y, 3):.3f} {round(r, 3):.3f}')


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
for i in range(n):
    lst = list(map(float, input().split()))
    a, b = lst[0], lst[1]
    c, d = lst[2], lst[3]
    e, f = lst[4], lst[5]
    
    px = ((a+c)*(a-c)*(b-f)-(a+e)*(a-e)*(b-d)+(b-d)*(b-f)*(d-f))/(2*((a-c)*(b-f)-(a-e)*(b-d)))
    py = ((a-c)*(a-e)*(c-e)+(b+d)*(b-d)*(a-e)-(b+f)*(b-f)*(a-c))/(2*((b-d)*(a-e)-(b-f)*(a-c)))
    r = ((a-px)**2 + (b-py)**2)**0.5


    print('{:.3f}'.format(px), end= " ")
    print('{:.3f}'.format(py), end= " ")
    print('{:.3f}'.format(r))

Solution (Python):
import math
for i in range(int(input())):
    x1,y1,x2,y2,x3,y3 = map(float,input().split())
    
    a = 2*(x2 - x1)
    b = 2*(y2 - y1)
    c = x1**2 - x2**2 + y1**2 - y2**2
    
    aa = 2*(x3 - x1)
    bb = 2*(y3 - y1)
    cc = x1**2 - x3**2 + y1**2 - y3**2

    x = (b*cc - bb*c) / (a*bb - aa*b)
    y = (c*aa - cc*a) / (a*bb - aa*b)
    
    r = math.hypot(x1 - x,y1 - y)
            
    print(f'{x:.03f} {y:.03f} {r:.03f}')

Solution (Python):
import math
N=int(input())
for i in range(N):
	x1,y1,x2,y2,x3,y3=[float(e) for e in input().split()]
	a1=2*x2-2*x1
	b1=2*y2-2*y1
	c1=x1*x1-x2*x2+y1*y1-y2*y2
	a2=2*x3-2*x1
	b2=2*y3-2*y1
	c2=x1*x1-x3*x3+y1*y1-y3*y3
	x=(b1*c2-b2*c1)/(a1*b2-a2*b1)
	y=(c1*a2-c2*a1)/(a1*b2-a2*b1)
	print('%.3f %.3f %.3f'%(x,y,math.hypot(x1-x,y1-y)))

Solution (Python):
import math
n = int(input())
    
for i in range(n) :
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    py = ((x3-x1)*(x1**2 + y1**2 - x2**2 - y2**2) - (x2-x1)*(x1**2 + y1**2 - x3**2 - y3**2)) / (2*(x3-x1)*(y1-y2) - 2*(x2-x1)*(y1-y3))
    if x1 == x2 :
        px = (2*(y1-y3)*py - x1**2 - y1**2 + x3**2 + y3**2) / (2*(x3 - x1))
    else :
        px = (2*(y1-y2)*py - x1**2 - y1**2 + x2**2 + y2**2) / (2*(x2 - x1))
    r = math.sqrt((px - x1)**2 + (py -y1)**2)

    print('{:.3f}'.format(px), '{:.3f}'.format(py), '{:.3f}'.format(r))

Solution (Python):
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); p = x1**2 - x2**2 + y1**2 - y2**2
    c = 2*(x1 - x3); d = 2*(y1 - y3); q = x1**2 - x3**2 + y1**2 - y3**2
    det = a*d - b*c
    x = d*p - b*q; y = a*q - c*p
    if det < 0:
        x = -x; y = -y; det = -det
    x /= det; y /= det
    r = ((x - x1)**2 + (y - y1)**2)**.5
    print("%.03f %.03f %.03f" % (x, y, r))

Solution (Python):
import math

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

def getCircle(x1, y1, x2, y2, x3, y3):
    d = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
    px = ((x1**2 + y1**2) * (y2 - y3) + (x2**2 + y2**2) * (y3 - y1) + (x3**2 + y3**2) * (y1 - y2) ) / d
    py = ((x1**2 + y1**2) * (x3 - x2) + (x2**2 + y2**2) * (x1 - x3) + (x3**2 + y3**2) * (x2 - x1) ) / d
    a = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
    b = math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
    c = math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
    s = (a + b + c) / 2
    A = math.sqrt(s * (s - a) * (s - b) * (s - c))
    r = a * b * c / (4 * A)
    return Circle(px,py,r)

          
n = int(input())
for k in range(n):
    x1, y1, x2, y2, x3, y3 = [float(x) for x in input().split()]
    circle = getCircle(x1, y1, x2, y2, x3, y3)
    print("%.3f %.3f %.3f" % (circle.x, circle.y, circle.r))

Solution (Python):
n = int(input())
for i in range(n):
    x1, y1, x2, y2, x3, y3 = list(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 = int(input())

round = lambda x:(x*1000*2+1)//2/1000

for i in range(n):
    x1,y1,x2,y2,x3,y3 = [ float(s) for s in input().split() ]
    r1 = x1*x1 + y1*y1
    r2 = x2*x2 + y2*y2
    r3 = x3*x3 + y3*y3
    x1_x2 = x1 - x2
    y1_y2 = y1 - y2
    x1_x3 = x1 - x3
    y1_y3 = y1 - y3
    r1_r2 = r1 - r2
    r1_r3 = r1 - r3
    if x1_x2 == 0:
        py = r1_r2 / (2*y1_y2)
        px = (r1_r3 - 2*y1_y3*py) / (2*x1_x3)
    else:
        py = (r1_r2 * x1_x3 - r1_r3 * x1_x2) / (2*(y1_y2 * x1_x3 - y1_y3 * x1_x2))
        px = (r1_r2 - 2*y1_y2*py) / (2*x1_x2)
    r = math.sqrt((x1-px)**2 + (y1-py)**2)
    print('{:.3f}'.format(0+px),'{:.3f}'.format(0+py),'{:.3f}'.format(0+round(r)))

Solution (Python):
def solve():
    from sys import stdin
    f_i = stdin
    
    n = int(f_i.readline())
    for i in range(n):
        x1, y1, x2, y2, x3, y3 = map(float, f_i.readline().split())
        A = x1 + y1 * 1j
        B = x2 + y2 * 1j
        C = x3 + y3 * 1j
        a = abs(C - B)
        b = abs(A - C)
        c = abs(B - A)
        c1 = a**2 * (b**2 + c**2 - a**2)
        c2 = b**2 * (c**2 + a**2 - b**2)
        c3 = c**2 * (a**2 + b**2 - c**2)
        
        U = (c1 * A + c2 * B + c3 * C) / (c1 + c2 + c3)
        R = abs(U - A)
        
        print('{:.3f} {:.3f} {:.3f}'.format(U.real, U.imag, R))

solve()