1406. Tilted Rectangles Count
A surveyor marks distinct stakes at the positions in points, each [x, y], and wants to know how many rectangles of positive area can be formed whose four corners are all marked stakes. The rectangles may be rotated to any angle, not only aligned with the axes. Return the count as a long.
The two diagonals of a rectangle share a midpoint and have the same length, and two different point pairs with the same midpoint and the same length are always the diagonals of exactly one rectangle. Group every unordered pair by (sum of its endpoints, squared length) and add C(g, 2) for each group of size g. The intended solution runs in O(n^2) average time and O(n^2) space.
Example 1
- Input:
- points = [[0,0],[1,0],[1,1],[0,1]]
- Output:
- 1
- Explanation:
The four points form one unit square, which is the only rectangle.
Example 2
- Input:
- points = [[1,0],[2,1],[1,2],[0,1],[1,1]]
- Output:
- 1
- Explanation:
The four outer points form a tilted square with the fifth point at its centre; no other four points form a rectangle.
Example 3
- Input:
- points = [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
- Output:
- 10
- Explanation:
The 3 by 3 lattice has 9 axis-aligned rectangles plus one tilted square through the edge midpoints, 10 in total.
Constraints
1 ≤ n ≤ 300
-104 ≤ x, y ≤ 104
All points are distinct.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n^2)
- Space
- O(n^2)