Watson gives a circle and a triangle in a 2-dimensional plane to Sherlock. Sherlock has to tell if they intersect/touch each other. 

The circle is centered at $(x_c,y_c)$ and has radius $\mbox{R}$.   

Input Format 

The first line contains $\mathbf{T}$, the number of test cases. 

Each test case consists of $x_c$, $\boldsymbol{y}_{c}$ and $\mbox{R}$ in one line. 

The next three lines each contains $x_i,y_i$ denoting the vertices of the triangle.   

Output Format 

For each test case, print YES if the triangle touches or intersects the circle; otherwise, print NO.   

Constraints 

$1\leq T\leq30000$ 

$1\leq R\leq2000$ 

$-2000\leq x_c,y_c\leq2000$ 

$-5000\leq x_i,y_i\leq5000$ 

Note: There will be no degenerate triangles (i.e. triangles with area 0)  

Sample Input  

2
0 0 10
10 0
15 0
15 5
0 0 10
0 0
5 0
5 5

Sample Output    

YES
NO

Explanation 

In the first case, the triangle is touching the circle. In the second case, it neither touches nor intersects the circle.

Difficulty: HARD

Solutions:
import math

def intersect(cx, cy, r, x1, y1, x2, y2):
	threshold = 0.0001
	dx = x2 - x1
	dy = y2 - y1
	t = math.sqrt(dx ** 2 + dy ** 2)
	(dx, dy) = (dx / t, dy / t)
	t2 = (dx * (cy - y1) - dy * (cx - x1)) / (dx ** 2 + dy ** 2)
	ix = cx + t2 * dy
	iy = cy - t2 * dx
	ri = math.sqrt((ix - cx) ** 2 + (iy - cy) ** 2)
	if ri > r + threshold:
		return False
	if abs(ri - r) < threshold:
		return True
	b = math.sqrt(r * r - ri * ri)
	mx1 = ix + b * dx
	my1 = iy + b * dy
	mx2 = ix - b * dx
	my2 = iy - b * dy
	maxx = max(x1, x2)
	minx = min(x1, x2)
	maxy = max(y1, y2)
	miny = min(y1, y2)
	return minx - threshold <= mx1 <= maxx + threshold and miny - threshold <= my1 <= maxy + threshold or (minx - threshold <= mx2 <= maxx + threshold and miny - threshold <= my2 <= maxy + threshold)
for _ in range(0, int(input())):
	circle = [int(x) for x in input().split(' ')]
	v = []
	for i in range(0, 3):
		v.append([int(x) for x in input().split(' ')])
	print('YES' if intersect(*circle + v[0] + v[1]) or intersect(*circle + v[1] + v[2]) or intersect(*circle + v[0] + v[2]) else 'NO')


from fractions import Fraction as F
t = int(input())

def check(x1, y1, x2, y2, r):
	(x, y) = (x1, y1)
	dx = x2 - x
	dy = y2 - y
	A = dx ** 2 + dy ** 2
	halfB = x * dx + y * dy
	opt = F(-halfB, A)
	if opt < 0:
		opt = 0
	elif opt > 1:
		opt = 1
	least_dist = (x + dx * opt) ** 2 + (y + dy * opt) ** 2
	max_dist = max(x1 ** 2 + y1 ** 2, x2 ** 2 + y2 ** 2)
	return least_dist <= r ** 2 <= max_dist
for _ in range(t):
	(x0, y0, r) = map(int, input().split())
	(x1, y1) = map(int, input().split())
	(x2, y2) = map(int, input().split())
	(x3, y3) = map(int, input().split())
	x1 -= x0
	y1 -= y0
	x2 -= x0
	y2 -= y0
	x3 -= x0
	y3 -= y0
	print('YES' if check(x1, y1, x2, y2, r) or check(x2, y2, x3, y3, r) or check(x3, y3, x1, y1, r) else 'NO')


from fractions import Fraction as F
t = int(input())

def check(x1, y1, x2, y2, r):
	(x, y) = (x1, y1)
	dx = x2 - x
	dy = y2 - y
	A = dx ** 2 + dy ** 2
	halfB = x * dx + y * dy
	opt = F(-halfB, A)
	if opt < 0:
		opt = 0
	elif opt > 1:
		opt = 1
	least_dist = (x + dx * opt) ** 2 + (y + dy * opt) ** 2
	max_dist = max(x1 ** 2 + y1 ** 2, x2 ** 2 + y2 ** 2)
	return least_dist <= r ** 2 <= max_dist
for _ in range(t):
	(x0, y0, r) = map(int, input().split())
	(x1, y1) = map(int, input().split())
	(x2, y2) = map(int, input().split())
	(x3, y3) = map(int, input().split())
	x1 -= x0
	y1 -= y0
	x2 -= x0
	y2 -= y0
	x3 -= x0
	y3 -= y0
	print('YES' if check(x1, y1, x2, y2, r) or check(x2, y2, x3, y3, r) or check(x3, y3, x1, y1, r) else 'NO')


from fractions import Fraction as F
t = int(input())

def check(x1, y1, x2, y2, r):
	(x, y) = (x1, y1)
	dx = x2 - x
	dy = y2 - y
	A = dx ** 2 + dy ** 2
	halfB = x * dx + y * dy
	opt = F(-halfB, A)
	if opt < 0:
		opt = 0
	elif opt > 1:
		opt = 1
	least_dist = (x + dx * opt) ** 2 + (y + dy * opt) ** 2
	max_dist = max(x1 ** 2 + y1 ** 2, x2 ** 2 + y2 ** 2)
	return least_dist <= r ** 2 <= max_dist
for _ in range(t):
	(x0, y0, r) = map(int, input().split())
	(x1, y1) = map(int, input().split())
	(x2, y2) = map(int, input().split())
	(x3, y3) = map(int, input().split())
	x1 -= x0
	y1 -= y0
	x2 -= x0
	y2 -= y0
	x3 -= x0
	y3 -= y0
	print('YES' if check(x1, y1, x2, y2, r) or check(x2, y2, x3, y3, r) or check(x3, y3, x1, y1, r) else 'NO')