Mid/SeniorGeometryMath

Valid Square

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, where each angle is 90 degrees.

Example 1
Inputp1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Outputtrue
The four points form a square with side length 1.
Example 2
Inputp1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Outputfalse
The four points do not form a square because the side lengths and angles are not all valid for a square.

Constraints

  • p1.length == p2.length == p3.length == p4.length == 2
  • -10^4 <= xi, yi <= 10^4

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate