Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a square.
The coordinate (x,y) of a point is represented by an integer array with two integers.
Example:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: TrueLet's draw out our square.

There are 2 things to note here:
These numbers do not have to be 0 or 1. They can be 500, 0 etc.
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
One way to do this is to calculate the number of unique distances. Let's visualise this.

The distance here is 1.

And here. This means for all 4 points, we have 1 unique distance (1 to each point) in this square.
But, if we include diagonals

We now have 2.
Using Pythagoras' Therom we can calculate the distance.
We then need to:
Number of unique distances = 2
We have 4 distances in total for our square
We have 2 distances for our diagonals
Last updated
Was this helpful?