#P1665. Count Squares Formed by Points
Count Squares Formed by Points
Count Squares Formed by Points
Given N points on a plane, compute the number of squares that can be formed using any four of these points as vertices. Note that the sides of a square do not necessarily have to be parallel to the coordinate axes.
A square has four equal sides and two pairs of parallel sides. Two common ways to form a square from a pair of points include treating the pair as adjacent vertices. For two points A and B with coordinates \( (x_1,y_1) \) and \( (x_2,y_2) \) respectively, let \( dx = x_2-x_1 \) and \( dy = y_2-y_1 \). Then the other two vertices can be computed as follows:
- Candidate 1 (90° counter-clockwise rotation): \( (x_1-dy,y_1+dx) \) and \( (x_2-dy,y_2+dx) \).
- Candidate 2 (90° clockwise rotation): \( (x_1+dy,y_1-dx) \) and \( (x_2+dy,y_2-dx) \).
Count each valid formation and ensure that each square is counted only once. It is recommended to divide the total count by 4, since every square is detected from four different edges.
inputFormat
The first line contains an integer \( N \) (with \( N \ge 4 \)) specifying the number of points. The next \( N \) lines each contain two integers representing the x and y coordinates of a point.
outputFormat
Output a single integer: the number of squares that can be formed using any four of the given points as vertices. Each square is counted only once.
sample
4
0 0
1 0
1 1
0 1
1