We are given a sequence of coplanar points and see all the possible triangles that may be generated which all combinations of three points.

We have the following list of points with the cartesian coordinates of each one:
```
Points [x, y]
   A   [1, 2]
   B   [3, 3]
   C   [4, 1]
   D   [1, 1]
   E   [4, -1]
```
With these points we may have the following triangles: ```ABC, ABD, ABE, ACD, ACE, ADE, BCD, BCE, BDE, CDE.``` There are three special ones: ```ABC, ACD and CDE```, that have an angle of 90°. All is shown in the picture below:



We need to count all the rectangle triangles that may be formed by a given list of points.

The case decribed above will be:
```python
count_rect_triang([[1, 2],[3, 3],[4, 1],[1, 1],[4, -1]]) == 3
```

Observe this case:
```python
count_rect_triang([[1, 2],[4, -1],[3, 3],[4, -1],[4, 1],[1, 1],[4, -1], [4, -1], [3, 3], [1, 2]]) == 3
```
If no rectangle triangles may be generated the function will output ```0```.

Enjoy it!

Source: codewars | Difficulty: EASY
Tags: Geometry

Fundamentals

Mathematics

Starter code: def count_rect_triang(points):
	

Solutions:
from itertools import combinations

def isRect(a, b, c):
	(X, Y, Z) = sorted((sum(((q - p) ** 2 for (p, q) in zip(p1, p2))) for (p1, p2) in [(a, b), (a, c), (b, c)]))
	return X + Y == Z

def count_rect_triang(points):
	return sum((isRect(*c) for c in combinations(set(map(tuple, points)), 3)))


from itertools import combinations

def count_rect_triang(points):
	result = 0
	for ((x1, y1), (x2, y2), (x3, y3)) in combinations(set(map(tuple, points)), 3):
		d1 = (x1 - x2) ** 2 + (y1 - y2) ** 2
		d2 = (x2 - x3) ** 2 + (y2 - y3) ** 2
		d3 = (x3 - x1) ** 2 + (y3 - y1) ** 2
		(d1, d2, d3) = sorted((d1, d2, d3))
		result += d1 + d2 == d3
	return result


from itertools import combinations

def ok(a, b, c):
	((x1, y1), (x2, y2), (x3, y3)) = (a, b, c)
	(d1, d2, d3) = ((x1 - x2) ** 2 + (y1 - y2) ** 2, (x1 - x3) ** 2 + (y1 - y3) ** 2, (x2 - x3) ** 2 + (y2 - y3) ** 2)
	(d1, d2, d3) = sorted([d1, d2, d3])
	return d1 + d2 == d3

def count_rect_triang(points):
	return sum((ok(pa, pb, pc) for (pa, pb, pc) in combinations(set(map(tuple, points)), 3)))


f = lambda t, r=2: [sorted, iter][r - 2]((abs(sum(p) - 2 * p[-1]) ** 2 for p in map([list, f][r - 2], __import__('itertools').combinations(t, r))))
count_rect_triang = lambda p: sum((d < 1e-09 for d in f({x + 1j * y for (x, y) in p}, 3)))


from itertools import combinations

def count_rect_triang(points):
	return sum((1 for ps in combinations(set(map(tuple, points)), 3) for (p0, p1, p2) in ((ps[0], ps[1], ps[2]), (ps[1], ps[2], ps[0]), (ps[2], ps[1], ps[0])) if (p1[0] - p0[0]) * (p2[0] - p0[0]) + (p1[1] - p0[1]) * (p2[1] - p0[1]) == 0))


from itertools import product

def count_rect_triang(points):
	return sum((1 for (p0, p1, p2) in product(set(map(tuple, points)), repeat=3) if p0 != p1 != p2 != p0 and (p1[0] - p0[0]) * (p2[0] - p0[0]) + (p1[1] - p0[1]) * (p2[1] - p0[1]) == 0)) // 2


def dist(ps):
	(p1, p2, p3) = ps
	x = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
	y = (p1[0] - p3[0]) ** 2 + (p1[1] - p3[1]) ** 2
	z = (p2[0] - p3[0]) ** 2 + (p2[1] - p3[1]) ** 2
	return sorted([x, y, z])
from itertools import combinations

def count_rect_triang(points):
	if len(points) < 3:
		return 0
	ps = []
	for i in points:
		if i not in ps:
			ps.append(i)
	c = 0
	for i in combinations(ps, 3):
		temp = dist(i)
		if round(temp[-1], 4) == round(temp[0] + temp[1], 4):
			c += 1
	return c


from itertools import combinations

def count_rect_triang(points):
	n = 0
	points_set = set()
	for p in points:
		points_set.add(tuple(p))
	for ((x1, y1), (x2, y2), (x3, y3)) in combinations(points_set, 3):
		(l1, l2, l3) = sorted([(x2 - x1) ** 2 + (y2 - y1) ** 2, (x3 - x2) ** 2 + (y3 - y2) ** 2, (x1 - x3) ** 2 + (y1 - y3) ** 2])
		if l1 + l2 == l3:
			n += 1
	return n


from itertools import combinations

def count_rect_triang(points):
	ans = 0
	for c in combinations(set(map(tuple, points)), 3):
		dist = [(p[0][0] - p[1][0]) ** 2 + (p[0][1] - p[1][1]) ** 2 for p in combinations(c, 2)]
		ans += sum(dist) == max(dist) * 2
	return ans