Imagine you're a quality inspector at a precision manufacturing company that creates square components. You're given the coordinates of four corner points in 2D space, but they're not in any particular order. Your job is to determine whether these points actually form a valid square.
A valid square must satisfy these geometric properties:
- Four equal sides with positive length
- Four equal angles (90-degree angles)
- All corners are distinct points
Input: Four points p1, p2, p3, p4 where each point is represented as [x, y] coordinates
Output: Return true if the four points construct a perfect square, false otherwise
Challenge: The points are given in random order, so you need to figure out which points are adjacent and which are diagonally opposite!
Input & Output
Visualization
Time & Space Complexity
Always calculate exactly 6 distances regardless of input
Only store the 6 distances and a few variables
Constraints
- p1.length == p2.length == p3.length == p4.length == 2
- -104 <= xi, yi <= 104
- All coordinates are integers
- Edge case: Points may be given in any order