Problem
Given the coordinates of four points in 2D space p1
, p2
, p3
and p4
, return true
if the four points construct a square.
The coordinate of a point pi
is represented as [xi, yi]
. The input is not given in any order.
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Solution
Method 1 - Using Constraint on square sides and diagonals
To determine if four points in a 2D space can form a square, we need to verify two main conditions:
- Equal Sides: All four sides of the square must be of equal length.
- Equal Diagonals: The diagonals must also be of equal length (as this ensures the angles are 90 degrees).
Appraoch
Here is an approach to solve this problem:
- Calculate all distances between the points.
- Sort these distances: In a valid square, there should be 4 smaller equal distances (sides of the square) and 2 larger equal distances (diagonals).
- Check the conditions: If there are 4 equal smaller distances and 2 equal larger distances, the points form a square.
Code
|
|
Complexity
- ⏰ Time complexity:
O(1)
- We calculate the distance between every pair of the four points.
- There are ( \binom{4}{2} = 6 ) pairs, so this operation involves 6 distance calculations.
- Each distance calculation involves basic arithmetic operations, which are ( O(1) ).
- 🧺 Space complexity:
O(1)
- We are just storing 2 values in map OR in worst case 6 values, but it is O(1)