283. Four Corners Square
A surveyor drives four stakes into a field and records each stake as an integer point [x, y]: p1, p2, p3 and p4. The stakes are listed in no particular order, so p1 and p2 need not be neighbours.
Return true if the four points are the corners of a square with positive area, which may be tilted at any angle, and false otherwise (a rectangle, a rhombus, repeated points or collinear points do not qualify). Compare the six squared pairwise distances in O(1) time and space; coordinates are limited to 10^7 in magnitude so squared distances stay within 64-bit and double precision.
Example 1
- Input:
- p1 = [2,0]p2 = [0,3]p3 = [-3,1]p4 = [-1,-2]
- Output:
- true
- Explanation:
A tilted square: all four sides have squared length 13 and both diagonals have squared length 26.
Example 2
- Input:
- p1 = [0,0]p2 = [6,0]p3 = [6,4]p4 = [0,4]
- Output:
- false
- Explanation:
This is a 6 x 4 rectangle, whose sides are not all equal.
Example 3
- Input:
- p1 = [5,5]p2 = [5,5]p3 = [5,5]p4 = [5,5]
- Output:
- false
- Explanation:
All four points coincide, so the area is zero.
Constraints
p1, p2, p3, p4 each have length 2
-107 ≤ x, y ≤ 107
Points may coincide.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms
Expected complexity
- Time
- O(1)
- Space
- O(1)