Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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



Sample input:
1
0.0 0.0 2.0 0.0 2.0 2.0


Sample output:
1.000 1.000 1.414

Submission status: Accepted

Solution code:
# Circumscribed Circle of a Triangle

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)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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) ] # deepcopy A_src -> A
    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 ["0.0 3.0 -1.0 0.0 -3.0 4.0"]: # expected [-2.000, 2.000, 2.236]
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])

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
#!/usr/bin/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)))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
def f1(a, b, c, d):  # 2点の中心を通り垂直な線の傾きと切片
    k = (a - c) / (d - b)
    h = ((b + d) - k * (a + c)) / 2
    return k, h


def f2(a, b, c, d):  # 2直線との交点
    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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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}')

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    x1, y1, x2, y2, x3, y3 = list(map(float, input().split()))
    a = 2*(x1-x2)
    b = 2*(y1-y2)
    c = x1**2 - x2**2 + y1**2 - y2**2
    d = 2*(x1-x3)
    e = 2*(y1-y3)
    f = x1**2 - x3**2 + y1**2 - y3**2

    y = (c*d - a*f) / (b*d - a*e)
    if a==0:
        x = (f - e*y) / d
    else:
        x = (c - b*y) / a
    
    r = ((x1 - x)**2 + (y1 - y)**2)**0.5
    print('{:.3f}'.format(round(x, 3)), end=" ")
    print('{:.3f}'.format(round(y, 3)), end=" ")
    print('{:.3f}'.format(round(r, 3)))

Submission status: Accepted

Solution code:
import math
 
n = int(input())
positions = []
for i in range(n):
  positions.append(list(map(float, input().split())))
  
for i in range(n):
  ra = [positions[i][0],positions[i][1]]
  rb = [positions[i][2],positions[i][3]]
  rc = [positions[i][4],positions[i][5]]
 
  A = (rb[0]-rc[0])**2+(rb[1]-rc[1])**2
  B = (rc[0]-ra[0])**2+(rc[1]-ra[1])**2
  C = (ra[0]-rb[0])**2+(ra[1]-rb[1])**2
 
  P = A * (B+C-A)
  Q = B * (C+A-B)
  R = C * (A+B-C)
 
  rcc_x = (P*ra[0] + Q*rb[0] + R*rc[0]) / (P+Q+R)
  rcc_y = (P*ra[1] + Q*rb[1] + R*rc[1]) / (P+Q+R)
 
  radius = math.sqrt((rcc_x-positions[i][0])**2+(rcc_y-positions[i][1])**2)
  print("{0:.3f} {1:.3f} {2:.3f}".format(rcc_x,rcc_y,radius))

Submission status: Accepted

Solution code:
import math
for _ in range(int(input())):
  x1,y1,x2,y2,x3,y3=map(float,input().split())
  a,b,c=2*(x2-x1),2*(y2-y1),x1**2-x2**2+y1**2-y2**2
  aa,bb,cc=2*(x3-x1),2*(y3-y1),x1**2-x3**2+y1**2-y3**2
  x,y=(b*cc-bb*c)/(a*bb-aa*b),(c*aa-cc*a)/(a*bb-aa*b)
  print('%.3f %.3f %.3f'%(x,y,math.hypot(x1-x,y1-y)))

Submission status: Accepted

Solution code:
import math
for _ in range(int(input())):
    x1, y1, x2, y2, x3, y3 = map(float, 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)-(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)-(x1-x2)*(y1-y3))
    r = math.sqrt((x1 - p) ** 2 + (y1 - q) ** 2)
    print("{:.3f} {:.3f} {:.3f}".format(p, q, r))

Submission status: Accepted

Solution code:
import math
for _ in range(int(input())):
    x1,y1,x2,y2,x3,y3=map(float,input().split())
    a,b,c=2*(x2-x1),2*(y2-y1),x1**2-x2**2+y1**2-y2**2
    aa,bb,cc=2*(x3-x1),2*(y3-y1),x1**2-x3**2+y1**2-y3**2
    x,y=(b*cc-bb*c)/(a*bb-aa*b),(c*aa-cc*a)/(a*bb-aa*b)
    print('%.3f %.3f %.3f'%(x,y,math.hypot(x1-x,y1-y)))

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
import math
n=int(input())
for i in range(n):
  x1,y1,x2,y2,x3,y3=map(float,input().split())
  a=(x3-x1)*(x1**2+y1**2-x2**2-y2**2)-(x2-x1)*(x1**2+y1**2-x3**2-y3**2)
  b=2*(x3-x1)*(y1-y2)-2*(x2-x1)*(y1-y3)
  py=a/b
  if x2-x1!=0:
    px=(2*(y1-y2)*py-x1**2-y1**2+x2**2+y2**2)/(2*(x2-x1))
  else:
    px=(2*(y1-y3)*py-x1**2-y1**2+x3**2+y3**2)/(2*(x3-x1))
  r=math.sqrt((px-x1)**2+(py-y1)**2)
  print(f"{px:.3f}",f"{py:.3f}",f"{r:.3f}")

Submission status: Accepted

Solution code:
import math

n = int(input())
positions = []
for i in range(n):
  positions.append(list(map(float, input().split())))

for i in range(n):
  ra = [positions[i][0],positions[i][1]]
  rb = [positions[i][2],positions[i][3]]
  rc = [positions[i][4],positions[i][5]]

  A = (rb[0]-rc[0])**2+(rb[1]-rc[1])**2
  B = (rc[0]-ra[0])**2+(rc[1]-ra[1])**2
  C = (ra[0]-rb[0])**2+(ra[1]-rb[1])**2

  P = A * (B+C-A)
  Q = B * (C+A-B)
  R = C * (A+B-C)

  rcc_x = (P*ra[0] + Q*rb[0] + R*rc[0]) / (P+Q+R)
  rcc_y = (P*ra[1] + Q*rb[1] + R*rc[1]) / (P+Q+R)

  radius = math.sqrt((rcc_x-positions[i][0])**2+(rcc_y-positions[i][1])**2)
  print("{0:.3f} {1:.3f} {2:.3f}".format(rcc_x,rcc_y,radius))

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP
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=x1**2+y1**2-x2**2-y2**2
    d=2*(x1-x3)
    e=2*(y1-y3)
    f=x1**2+y1**2-x3**2-y3**2
    x=(c*e-b*f)/(a*e-b*d)+0.0
    y=(c*d-a*f)/(b*d-a*e)+0.0
    r=((x-x1)**2+(y-y1)**2)**0.5
    print(Decimal(str(x)).quantize(Decimal('0.001'), rounding=ROUND_HALF_UP),Decimal(str(y)).quantize(Decimal('0.001'), rounding=ROUND_HALF_UP),Decimal(str(r)).quantize(Decimal('0.001'), rounding=ROUND_HALF_UP))

Submission status: Accepted

Solution code:
n = int(input())
import math
for _ in range(n):
    ax,ay,bx,by,cx,cy = [float(i) for i in input().split()]
    ab = math.sqrt((ax-bx)**2+(ay-by)**2)
    bc = math.sqrt((bx-cx)**2+(by-cy)**2)
    ca = math.sqrt((cx-ax)**2+(cy-ay)**2)
    cos_a = (ab**2+ca**2-bc**2)/(2*ab*ca)
    sin_a = math.sqrt(1-cos_a**2)
    r = bc/(2*sin_a)
    px = ((ay-cy)*(ay**2 -by**2 +ax**2 -bx**2) -(ay-by)*(ay**2 -cy**2 +ax**2 -cx**2)) / (2*(ay-cy)*(ax-bx)-2*(ay-by)*(ax-cx))
    py = ((ax-cx)*(ax**2 -bx**2 +ay**2 -by**2) -(ax-bx)*(ax**2 -cx**2 +ay**2 -cy**2)) / (2*(ax-cx)*(ay-by)-2*(ax-bx)*(ay-cy))
    def round3(x):
        x_ = x*10**3
        if x_%1 < 0.5:
            x_ = math.floor(x_)
        else:
            x_ = math.ceil(x_)
        return x_*10**(-3)
        
            
    print("{:.3f} {:.3f} {:.3f}".format(round3(px),round3(py),round3(r)))

Submission status: Accepted

Solution code:
import math
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP

n = int(input())

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

    a2 = (x2-x3)**2 + (y2-y3)**2
    b2 = (x1-x3)**2 + (y1-y3)**2
    c2 = (x1-x2)**2 + (y1-y2)**2
    area = ((x2-x1) * (y3-y1) - (x3-x1) * (y2-y1)) / 2

    x = (a2*(b2+c2-a2)*x1 + b2*(c2+a2-b2) *
         x2 + c2*(a2+b2-c2)*x3) / (16*area*area)
    y = (a2*(b2+c2-a2)*y1 + b2*(c2+a2-b2) *
         y2 + c2*(a2+b2-c2)*y3) / (16*area*area)
    r = ((x1-x)**2 + (y1-y)**2)**0.5

    x = Decimal(str(x)).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
    y = Decimal(str(y)).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
    r = Decimal(str(r)).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)

    print(x, y, r)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def distance(x1, y1, x2, y2):
    return math.sqrt((x1-x2)**2 + (y1-y2)**2)

def intersection(a1, a2, b1, b2):
    x1,y1 = a1
    x2,y2 = a2
    x3,y3 = b1
    x4,y4 = b2

    ksi = (y4 - y3) * (x4 - x1) - (x4 - x3) * (y4 - y1)
    eta = (x2 - x1) * (y4 - y1) - (y2 - y1) * (x4 - x1)
    delta = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3)
    if delta == 0:
        return None
    ramda = ksi / delta;
    mu = eta / delta;
    if ramda >= 0 and ramda <= 1 and mu >= 0 and mu <= 1:
        return (x1 + ramda * (x2 - x1), y1 + ramda * (y2 - y1))

    return None

def circumcenters(a,b,c):
    t1 = [(a[0]+b[0])/2, (a[1]+b[1])/2]
    s1 = [t1[1]-a[1], a[0]-t1[0]]
    t2 = [(a[0]+c[0])/2, (a[1]+c[1])/2]
    s2 = [t2[1]-a[1], a[0]-t2[0]]
    p1 = [t1[0]+s1[0]*1e7, t1[1]+s1[1]*1e7]
    p2 = [t1[0]-s1[0]*1e7, t1[1]-s1[1]*1e7]
    p3 = [t2[0]+s2[0]*1e7, t2[1]+s2[1]*1e7]
    p4 = [t2[0]-s2[0]*1e7, t2[1]-s2[1]*1e7]
    return intersection(p1,p2,p3,p4)

def main():
    n = I()
    rr = []
    for _ in range(n):
        x1,y1,x2,y2,x3,y3 = LF()
        a = [x1,y1]
        b = [x2,y2]
        c = [x3,y3]
        t = circumcenters(a,b,c)
        rr.append('{:0.3f} {:0.3f} {:0.3f}'.format(t[0], t[1], distance(t[0],t[1],x1,y1)))

    return '\n'.join(rr)


print(main())

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    x1,y1,x2,y2,x3,y3 = map(float, input().split())
    gx = (x1+x2+x3)/3
    gy = (y1+y2+y3)/3
    G=( y2*x1-y1*x2 +y3*x2-y2*x3 +y1*x3-y3*x1 )
    Xc= ((x1*x1+y1*y1)*(y2-y3)+(x2*x2+y2*y2)*(y3-y1)+(x3*x3+y3*y3)*(y1-y2))/(2*G)
    Yc=-((x1*x1+y1*y1)*(x2-x3)+(x2*x2+y2*y2)*(x3-x1)+(x3*x3+y3*y3)*(x1-x2))/(2*G)
    r = ( (x1 - Xc) * (x1 - Xc) + (y1 - Yc) * (y1 - Yc) )**0.5
    print("%.3f %.3f %.3f"%(Xc,Yc,r))

Submission status: Accepted

Solution code:
def circumcenter(vert1, vert2, vert3):
    # H/T: wikipedia.org/wiki/Circumscribed_circle
    # refer from https://gist.github.com/dhermes/9ce057da49df63345c33
    Ax, Ay = vert1
    Bx, By = vert2
    Cx, Cy = vert3

    D = 2 * (Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By))
    norm_A = Ax**2 + Ay**2
    norm_B = Bx**2 + By**2
    norm_C = Cx**2 + Cy**2

    Ux = norm_A * (By - Cy) + norm_B * (Cy - Ay) + norm_C * (Ay - By)
    Uy = -(norm_A * (Bx - Cx) + norm_B * (Cx - Ax) + norm_C * (Ax - Bx))

    r = (Ax - Ux/D)*(Ax - Ux/D) + (Ay - Uy/D)* (Ay - Uy/D)
    r = float(r)**0.5
    return [Ux/D, Uy/D, r]

n  = int(input())
for i in range(n):
  xs = list(map(float, input().split()))
  a = circumcenter(xs[0:2], xs[2:4], xs[4:6])
  print(' '.join(map(lambda x:f'{x:0.03f}', a)))

Submission status: Accepted

Solution code:
n = int(input())
ans = []
for i in range(n):
    ax,ay,bx,by,cx,cy = map(float, input().split(" "))
    a,b,c = 2*(ax-bx),2*(ay-by),(ax+bx)*(ax-bx)+(ay+by)*(ay-by)
    d,e,f = 2*(ax-cx),2*(ay-cy),(ax+cx)*(ax-cx)+(ay+cy)*(ay-cy)
    det = a*e-b*d
    px = (c*e-b*f)/det
    py = (a*f-c*d)/det
    if abs(px) < 1e-4:
        px = 0.0
    if abs(py) < 1e-4:
        py = 0.0
    r = ((ax-px)**2+(ay-py)**2)**(1/2)
    ans.append([px,py,r])
for i in range(n):
    print("{0:.3f}".format(ans[i][0]),"{0:.3f}".format(ans[i][1]),"{0:.3f}".format(ans[i][2]))

Submission status: Accepted

Solution code:
# Circumscribed Circle of a Triangle
import math
def simul_eq(a,b,c,d,e,f):
    # A = [[a,b],[d,e]]
    C = [c,f]
    detA = a*e - b*d
    # if detA == 0: raise # det(A) == 0.
    At = [[e,-b],[-d,a]]
    x = sum(map((lambda x,y: x*y), At[0], C)) / detA
    y = sum(map((lambda x,y: x*y), At[1], C)) / detA
    fx,fy = '{0:.3f}'.format(x), '{0:.3f}'.format(y)
    if fx == '-0.000': fx = fx[1:]
    if fy == '-0.000': fy = fy[1:]
    return (fx,fy)

n = int(input())
for _ in range(n):
    x1,y1,x2,y2,x3,y3 = map(float, input().split())
    a = math.sqrt(pow(x2-x3, 2) + pow(y2-y3, 2))
    b = math.sqrt(pow(x1-x3, 2) + pow(y1-y3, 2))
    c = math.sqrt(pow(x2-x1, 2) + pow(y2-y1, 2))
    cosA = (pow(b,2) + pow(c,2) - pow(a,2)) / (2*b*c)
    sinA = math.sqrt(1 - pow(cosA,2))
    R = a / (2 * sinA)
    cen = simul_eq(x2-x1, y2-y1, (y2**2 - y1**2 + x2**2 - x1**2) / 2, x3-x1, y3-y1, (y3**2 - y1**2 + x3**2 - x1**2) / 2)
    print(' '.join(cen), '{0:.3f}'.format(R))

Submission status: Runtime Error

Solution code:
import math

def run():
    n = int(input())
    for _ in range(n):
        x1, y1, x2, y2, x3, y3 = list(map(float, input().split()))
        u1 = (x1-x3)*(x2-x1)*(x3-x2)
        u2 = (x2-x1)*(y3-y2)*(y3+y2)
        u3 = (x3-x2)*(y2-y1)*(y2+y1)
        v = x1*x2 + x2*y3 + x3*y1 - x1*y3 - x2*y1 + x3*y2
        Py = (-u1 + u2 - u3) / v / 2
        Px = (x2**2 - x1**2 + y2**2 - y1**2 - (y2 - y1)*Py) / (x2-x1) / 2
        r = math.sqrt((Px-x1)**2 + (Py-y1)**2)
        print('{0:.3f} {1:.3f} {2:.3f}'.format(round(Px,3), round(Py,3), round(r,3)))

if __name__ == '__main__':
    run()

Submission status: Runtime Error

Solution code:
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
#include <cmath>
#include <climits>
#include <set>
#include <iostream>
#include <map>
#include <functional>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <complex>
#include <sstream>
#include <stack>

using namespace std;

#define reep(i, f, n) for (int i = f; i < n; ++i)
#define rep(i, n) reep(i, 0, n)


int main(){
    float x1,x2,x3,y1,y2,y3,gx,gy,G,Xc,Yc,r;
    int n;
    scanf("%d", &n);
    for (int i=0;i<n;i++) {
        scanf("%f %f %f %f %f %f", &x1, &y1, &x2, &y2, &x3, &y3);
        gx = (x1+x2+x3)/3;
        gy = (y1+y2+y3)/3;
        G =( y2*x1-y1*x2 +y3*x2-y2*x3 +y1*x3-y3*x1 );
        Xc = ((x1*x1+y1*y1)*(y2-y3)+(x2*x2+y2*y2)*(y3-y1)+(x3*x3+y3*y3)*(y1-y2))/(2*G);
        Yc =-((x1*x1+y1*y1)*(x2-x3)+(x2*x2+y2*y2)*(x3-x1)+(x3*x3+y3*y3)*(x1-x2))/(2*G);
        r = sqrt( (x1 - Xc) * (x1 - Xc) + (y1 - Yc) * (y1 - Yc) );
        printf("%.3f %.3f %.3f\n", Xc, Yc, r);
    }
    return EXIT_SUCCESS;
}

Submission status: Runtime Error

Solution code:
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)

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:.4f} {1:.4f} {2:.4f}".format(center.real, center.imag, T.circumscribedCircleRadius()))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

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)

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:.4f} {1:.4f} {2:.4f}".format(center.real, center.imag, T.circumscribedCircleRadius()))