A circle is defined by three coplanar points that are not aligned.

You will be given a list of circles and a point [xP, yP]. You have to create a function, ```count_circles()``` (Javascript ```countCircles()```), that will count the amount of circles that contains the point P inside (the circle border line is included).

```python
list_of_circles = ([[[-3,2], [1,1], [6,4]], [[-3,2], [1,1], [2,6]], [[1,1], [2,6], [6,4]], [[[-3,2],[2,6], [6,4]]]

point1 = [1, 4] # P1
count_circles(list_of_circles, point1) == 4 #(The four circles have P1 inside)
```

It may happen that the point may be external to all the circles.

```python
list_of_circles = ([[[-3,2], [1,1], [6,4]], [[-3,2], [1,1], [2,6]], [[1,1], [2,6], [6,4]], [[-3,2],[2,6], [6,4]]]

point2 = [10, 6] # P2
count_circles(list_of_circles, point2) == 0 #(P2 is exterior to the four circles)
```
The point may be in the circle line and that will be consider as an internal point of it, too. 
For practical purposes a given point ```P``` will be in the circle line if: 
         |r - d|/r < 10^(-10)

```r```: radius of the circle that should be calculated from the coordinates of the three given points.

```d```: distance from the point ```P``` to the center of the circle. Again we have to do a calculation, the coordinates of the center should be calculated using the coordinates of the three given points.

Let's see a case when the pints is in the circle line.

```python
list_of_circles = ([[[-3,2], [1,1], [6,4]], [[-3,2], [1,1], [2,6]], [[1,1], [2,6], [6,4]], [[[-3,2],[2,6], [6,4]]]

point3 = point2 = [2, 6] # P3
count_circles(list_of_circles, point3) == 4 #(P3  is an internal point of the four circles)
```

All these three cases are shown in the image below:



Your code should be able to skip these cases:

- inexistent circle when we have three points aligned

- undefined circles when two or three of given points coincides.

First ten people to solve it will receive extra points.

Hints: This kata will give you important formulas: ```Give The Center And The Radius of Circumscribed Circle. (A warm up challenge)```
```http://www.codewars.com/kata/give-the-center-and-the-radius-of-circumscribed-circle-a-warm-up-challenge```

Features of the tests:

```N: amount of Tests```

```n: amount of given circles```
```x, y: coordinates of the points that define the circle```
```xP, yP: coordinates of the point P```
```N = 500```
```10 < n < 500```
```-500 < x < 500, -500 < y < 500```
```-750 < xP < -750, -750 < yP < -750```

Difficulty: EASY
Tags: Fundamentals

Data structures

Mathematics
Skill types: Data structures

Starter code:
def count_circles(list_of_circles, point):
	

Solutions:
def circum_curvat(points):
	(A, B, C) = [complex(*p) for p in points]
	(BC, CA, AB) = (B - C, C - A, A - B)
	D = 2.0 * (A.real * BC + B.real * CA + C.real * AB).imag
	if not D:
		return (D, D)
	U = (abs(A) ** 2 * BC + abs(B) ** 2 * CA + abs(C) ** 2 * AB) / D
	radius = abs(BC) * abs(CA) * abs(AB) / abs(D)
	return (-1j * U, radius)

def count_circles(circles, point):
	return sum((abs(complex(*point) - center) < radius for (center, radius) in map(circum_curvat, circles)))


def count_circles(circles, point):
	return sum((inside(circle_xyr(circle), point) for circle in circles))

def inside(xyr, pt):
	if xyr is None:
		return False
	(x, y, r) = xyr
	return dist(pt, (x, y)) <= r

def circle_xyr(pts):
	if len(pts) != 3:
		return None
	(m1, c1) = perpbisector(pts[0], pts[1])
	(m2, c2) = perpbisector(pts[0], pts[2])
	if m1 == m2:
		return None
	(x, y) = (c1, m2 * c1 + c2) if m1 is None else (c2, m1 * c2 + c1) if m2 is None else ((c2 - c1) / (m1 - m2), m1 * (c2 - c1) / (m1 - m2) + c1)
	return (x, y, dist(pts[0], (x, y)))

def perpbisector(a, b):
	(ax, ay) = a
	(bx, by) = b
	(mpx, mpy) = ((ax + bx) / 2.0, (ay + by) / 2.0)
	if ax == bx:
		return (0, mpy)
	if ay == by:
		return (None, mpx)
	m = (ax - bx) / float(by - ay)
	c = mpy - m * mpx
	return (m, c)

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


def count_circles(list_of_circles, point, count=0):
	return sum((1 for circle in list_of_circles if point_in(circle, point)))

def point_in(points, point):
	helper = lambda x, y: (-1) ** y * (points[(x + 1) % 3][(y + 1) % 2] - points[(x + 2) % 3][(y + 1) % 2])
	D = 2 * sum((points[i][0] * helper(i, 0) for i in range(3)))
	U = [1.0 * sum((sum((points[i][j] ** 2 for j in range(2))) * helper(i, k) for i in range(3))) / D for k in range(2)]
	return compare(point, U, radius(points, D)) if not invalid(points, D) else False
compare = lambda p, U, r: distance(U, p) < r or abs(r - distance(U, p)) / r < 10 ** (-10)
distance = lambda p1, p2: ((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2) ** 0.5
invalid = lambda p, D: D == 0 or p[0] == p[1] or p[0] == p[2] or (p[1] == p[2])
radius = lambda p, D: distance(p[0], p[1]) * distance(p[0], p[2]) * distance(p[1], p[2]) / abs(D)


import math

def count_circles(loc, point):
	c = 0
	(px, py) = point
	for i in range(0, len(loc)):
		circle = circum_curvat(loc[i][0], loc[i][1], loc[i][2])
		if circle != False:
			(x, y, r) = circle
			d = ((x - px) ** 2 + (y - py) ** 2) ** 0.5
			if d <= r + 1e-10:
				c += 1
	return c

def circum_curvat(pointA, pointB, pointC):
	(xA, yA) = pointA
	(xB, yB) = pointB
	(xC, yC) = pointC
	if pointA == pointB or pointA == pointC or pointC == pointB:
		return False
	x = (xA ** 2 + yA ** 2) * (yB - yC) + (xB ** 2 + yB ** 2) * (yC - yA) + (xC ** 2 + yC ** 2) * (yA - yB)
	y = (xA ** 2 + yA ** 2) * (xC - xB) + (xB ** 2 + yB ** 2) * (xA - xC) + (xC ** 2 + yC ** 2) * (xB - xA)
	AB = ((xB - xA) ** 2 + (yB - yA) ** 2) ** 0.5
	BC = ((xC - xB) ** 2 + (yC - yB) ** 2) ** 0.5
	AC = ((xC - xA) ** 2 + (yC - yA) ** 2) ** 0.5
	D = 2.0 * (xA * (yB - yC) + xB * (yC - yA) + xC * (yA - yB))
	if D == 0:
		return False
	Ux = x / D
	Uy = y / D
	if D < 0:
		D = -D
	diam = AB * BC * AC / D
	return [Ux, Uy, diam]


def dist(pointA, pointB):
	return ((pointB[0] - pointA[0]) ** 2 + (pointB[1] - pointA[1]) ** 2) ** 0.5

def contains(circle, point):
	return circle and 0 <= circle['r'] - dist(point, circle['c']) <= circle['r'] or abs(circle['r'] - dist(point, circle['c'])) / circle['r'] < 1e-10

def circum_curvat(points):
	if len(set(map(tuple, points))) != 3:
		return None
	(xa, ya, xb, yb, xc, yc) = (points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1])
	D = 2.0 * (xa * (yb - yc) + xb * (yc - ya) + xc * (ya - yb))
	if not D:
		return None
	Ux = ((xa ** 2 + ya ** 2) * (yb - yc) + (xb ** 2 + yb ** 2) * (yc - ya) + (xc ** 2 + yc ** 2) * (ya - yb)) / D
	Uy = ((xa ** 2 + ya ** 2) * (xc - xb) + (xb ** 2 + yb ** 2) * (xa - xc) + (xc ** 2 + yc ** 2) * (xb - xa)) / D
	rad = dist(points[0], points[1]) * dist(points[1], points[2]) * dist(points[0], points[2]) / abs(D)
	return {'c': (Ux, Uy), 'r': rad}

def count_circles(list_of_circles, point):
	return len(filter(None, (contains(circum_curvat(p), point) for p in list_of_circles)))


import math

def get_center_radius(points):
	x = [pt[0] for pt in points]
	y = [pt[1] for pt in points]
	A = x[0] * (y[1] - y[2]) - y[0] * (x[1] - x[2]) + x[1] * y[2] - x[2] * y[1]
	B = (x[0] ** 2 + y[0] ** 2) * (y[2] - y[1]) + (x[1] ** 2 + y[1] ** 2) * (y[0] - y[2]) + (x[2] ** 2 + y[2] ** 2) * (y[1] - y[0])
	C = (x[0] ** 2 + y[0] ** 2) * (x[1] - x[2]) + (x[1] ** 2 + y[1] ** 2) * (x[2] - x[0]) + (x[2] ** 2 + y[2] ** 2) * (x[0] - x[1])
	D = (x[0] ** 2 + y[0] ** 2) * (x[2] * y[1] - x[1] * y[2]) + (x[1] ** 2 + y[1] ** 2) * (x[0] * y[2] - x[2] * y[0]) + (x[2] ** 2 + y[2] ** 2) * (x[1] * y[0] - x[0] * y[1])
	return (-B / (2 * A), -C / (2 * A), ((B ** 2 + C ** 2 - 4 * A * D) / (4 * A ** 2)) ** 0.5)

def count_circles(list_of_circles, point):
	counter = 0
	for circle in list_of_circles:
		(xc, yc, r) = get_center_radius(circle)
		d = math.hypot(xc - point[0], yc - point[1])
		counter += 1 if d < r or abs(d - r) / r < 10 ** (-10) else 0
	return counter


import math

def circum_curvat(pointA, pointB, pointC):
	D = 2 * (pointA[0] * (pointB[1] - pointC[1]) + pointB[0] * (pointC[1] - pointA[1]) + pointC[0] * (pointA[1] - pointB[1]))
	if abs(D) < 1e-10:
		return (0, 0, 0)
	x = ((pointA[0] ** 2 + pointA[1] ** 2) * (pointB[1] - pointC[1]) + (pointB[0] ** 2 + pointB[1] ** 2) * (pointC[1] - pointA[1]) + (pointC[0] ** 2 + pointC[1] ** 2) * (pointA[1] - pointB[1])) / D
	y = ((pointA[0] ** 2 + pointA[1] ** 2) * (pointC[0] - pointB[0]) + (pointB[0] ** 2 + pointB[1] ** 2) * (pointA[0] - pointC[0]) + (pointC[0] ** 2 + pointC[1] ** 2) * (pointB[0] - pointA[0])) / D
	ab = math.hypot(pointB[0] - pointA[0], pointB[1] - pointA[1])
	bc = math.hypot(pointB[0] - pointC[0], pointB[1] - pointC[1])
	ac = math.hypot(pointC[0] - pointA[0], pointC[1] - pointA[1])
	radius = ab * bc * ac / abs(D)
	return (x, y, radius)

def count_circles(list_of_circles, point):
	result = 0
	for circles in list_of_circles:
		(x, y, r) = circum_curvat(*circles)
		if r - math.hypot(point[0] - x, point[1] - y) > -1e-10:
			result += 1
	return result


from __future__ import division
from numpy import *

def find_circle(a, b, c):
	if any(map(array_equal, (a, b, c), (b, c, a))):
		return
	((ax, ay), (bx, by), (cx, cy)) = (a, b, c)
	d = ax * (by - cy) + bx * (cy - ay) + cx * (ay - by) << 1
	if not d:
		return
	(t1, t2, t3) = (ax ** 2 + ay ** 2, bx ** 2 + by ** 2, cx ** 2 + cy ** 2)
	x = (t1 * (by - cy) + t2 * (cy - ay) + t3 * (ay - by)) / d
	y = (t1 * (cx - bx) + t2 * (ax - cx) + t3 * (bx - ax)) / d
	(ab, bc, ca) = (linalg.norm(a - b), linalg.norm(b - c), linalg.norm(c - a))
	return (x, y, ab * bc * ca / abs(d))

def count_circles(list_of_circles, point):
	((px, py), result) = (point, 0)
	for circle in list_of_circles:
		if point in circle:
			result += 1
		else:
			res = find_circle(*map(array, circle))
			result += res and (px - res[0]) ** 2 + (py - res[1]) ** 2 <= res[2] ** 2
	return result


def count_circles(list_of_circles, point):
	c = 0
	for circle in list_of_circles:
		((x1, y1), (x2, y2), (x3, y3)) = (circle[0], circle[1], circle[2])
		x = (x2 ** 2 + y2 ** 2 - x1 ** 2 - y1 ** 2) * (y3 - y1) - (x3 ** 2 + y3 ** 2 - x1 ** 2 - y1 ** 2) * (y2 - y1)
		x /= 2.0 * ((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1))
		y = (x2 ** 2 + y2 ** 2 - x1 ** 2 - y1 ** 2) * (y3 - y1) - 2 * (x2 - x1) * (y3 - y1) * x
		y /= 2.0 * (y2 - y1) * (y3 - y1)
		r = ((x1 - x) ** 2 + (y1 - y) ** 2) ** 0.5
		d = ((x - point[0]) ** 2 + (y - point[1]) ** 2) ** 0.5
		if d <= r or abs(r - d) <= 1e-09:
			c += 1
	return c


from math import sqrt, hypot

def count_circles(list_of_circles, point):
	count = int(any((point in c for c in list_of_circles)))
	for [[x1, y1], [x2, y2], [x3, y3]] in list_of_circles:
		A = x1 * (y2 - y3) - y1 * (x2 - x3) + x2 * y3 - x3 * y2
		B = (x1 * x1 + y1 * y1) * (y3 - y2) + (x2 * x2 + y2 * y2) * (y1 - y3) + (x3 * x3 + y3 * y3) * (y2 - y1)
		C = (x1 * x1 + y1 * y1) * (x2 - x3) + (x2 * x2 + y2 * y2) * (x3 - x1) + (x3 * x3 + y3 * y3) * (x1 - x2)
		D = (x1 * x1 + y1 * y1) * (x3 * y2 - x2 * y3) + (x2 * x2 + y2 * y2) * (x1 * y3 - x3 * y1) + (x3 * x3 + y3 * y3) * (x2 * y1 - x1 * y2)
		[A, B, C, D] = map(float, [A, B, C, D])
		(xc, yc) = (-B / (2 * A), -C / (2 * A))
		r = sqrt((B * B + C * C - 4 * A * D) / (4 * A * A))
		d = hypot(point[0] - xc, point[1] - yc)
		if d <= r:
			count += 1
	return count