#P9423. Counting Isosceles Triangles Among Points
Counting Isosceles Triangles Among Points
Counting Isosceles Triangles Among Points
Given n points in the 2D coordinate plane, count the number of ways to choose three distinct points that form an isosceles triangle. A triangle is isosceles if at least two of its sides have equal length. In other words, if the three chosen points are P1, P2, and P3 with pairwise squared distances \(d_{12}\), \(d_{13}\), and \(d_{23}\), then the triangle is isosceles if one of the following holds:
[ \begin{aligned} &d_{12} = d_{13} \quad \text{or}\ &d_{12} = d_{23} \quad \text{or}\ &d_{13} = d_{23} \end{aligned} ]
Note: An equilateral triangle (where all three sides are equal) is a special kind of isosceles triangle. However, when counting the triangles using a method that fixes a vertex as the apex (where the equal sides meet), an equilateral triangle would be counted three times (once for each vertex). To account for this, if we denote:
[ S = \text{sum over all points of } \sum_{d} \binom{\text{count}_d}{2}, ]
and let \(E\) be the number of equilateral triangles, then the correct number of distinct isosceles triangles is:
[ \text{Answer} = S - 2E. ]
Your task is to compute the number of different isosceles triangles that can be formed from the given points.
inputFormat
The first line contains an integer n (the number of points). Following that, there are n lines. Each of these lines contains two space-separated integers representing the x and y coordinates of a point.
outputFormat
Output a single integer which is the count of isosceles triangles that can be formed from the given points.
sample
3
0 0
2 0
1 1
1