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: True
Note:
The idea behind determining whether 4 given set of points constitute a valid square or not is really simple. Firstly, we need to determine if the sides of the qaudrilateral formed by these 4 points are equal. But checking only this won\'t suffice. Since, this condition will be satisfied even in the case of a rhombus, where all the four sides are equal but the adjacent sides aren\'t perpendicular to each other. Thus, we also need to check if the lengths of the diagonals formed between the corners of the quadrilateral are equal. If both the conditions are satisfied, then only the given set of points can be deemed appropriate for constituting a square.
\nNow, the problem arises in determining which pairs of points act as the adjacent points on the square boundary. So, the simplest method is to consider every possible case. For the given 4 points, , there are a total of 4! ways in which these points can be arranged to be considered as the square\'s boundaries. We can generate every possible permutation and check if any permutation leads to the valid square arrangement of points.
\n\nComplexity Analysis
\nTime complexity : . Constant number of permutations() are generated.
\nSpace complexity : . Constant space is required.
\nInstead of considering all the permutations of arrangements possible, we can make use of maths to simplify this problem a bit. If we sort the given set of points based on their x-coordinate values, and in the case of a tie, based on their y-coordinate value, we can obtain an arrangement, which directly reflects the arrangement of points on a valid square boundary possible.
\nConsider the only possible cases as shown in the figure below:
\nIn each case, after sorting, we obtain the following conclusion regarding the connections of the points:
\n\n, , and form the four sides of any valid square.
\n\n and form the diagonals of the square.
\nThus, once the sorting of the points is done, based on the above knowledge, we can directly compare , , and for equality of lengths(corresponding to the sides); and and for equality of lengths(corresponding to the diagonals).
\n\nComplexity Analysis
\nTime complexity : . Sorting 4 points takes constant time.
\nSpace complexity : . Constant space is required.
\nAlgorithm
\nIf we consider all the permutations descripting the arrangement of points as in the brute force approach, we can come up with the following set of 24 arrangements:
\nIn this figure, the rows with the same shaded color indicate that the corresponding arrangements lead to the same set of edges and diagonals. Thus, we can see that only three unique cases exist. Thus, instead of generating all the 24 permutations, we check for the equality of edges and diagonals for only the three distinct cases.
\n\nComplexity Analysis
\nTime complexity : . A fixed number of comparisons are done.
\nSpace complexity : . No extra space required.
\nAnalysis written by: @vinod23
\n