Given the coordinates of any two vertices of a square (X1, Y1) and (X2, Y2), the task is to find the coordinates of the other two vertices. If a square cannot be formed using these two vertices, print -1.
Input: X1 = 1, Y1 = 2, X2 = 3, Y2 = 4
Output: (1, 4), (3, 2)
From the above figure the other two vertices of the square will be (1, 4) and (3, 2).
Input: X1 = -5, Y1 = 5, X2 = 5, Y2 = -5
Output: (-5, -5), (5, 5)
Approach: The approach is based on the fact that the length of all the sides of a square are equal. If no such vertices can be obtained for which the length of all the sides become equal, then print “-1”. Follow the steps below to solve the problem:
- The given two vertices can either be the vertices of the side of the square or the vertices of the diagonal.
- If the x-coordinates of the given two vertices are equal then the coordinates of the other two vertices will be:
(X1 + Y2 – Y1, Y1) and (X2 + Y2 – Y1, Y2)
- If the y-coordinates of the given two vertices are equal, then the coordinates of the other two vertices will be:
(X1, Y1 + X2 – X1x1) and (X2, Y2 + X2 – X1)
- Otherwise, the given coordinates are of the diagonal of the square. Therefore, the coordinates of the other two vertices will be:
(X1, Y2) and (X2, Y1)
Below is the implementation of the above approach:
1, 4 3, 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find two vertices of an isosceles triangle in which there is rectangle with opposite corners (0, 0) and (X, Y)
- Find the remaining balance after the transaction
- Program to find the time remaining for the day to complete
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Count square and non-square numbers before n
- Program to print Square inside a Square
- Solid square inside a hollow square | Pattern
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is a perfect square having all its digits as a perfect square
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given
- Maximize the sum of products of the degrees between any two vertices of the tree
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Minimum number of points to be removed to get remaining points on one side of axis
- Append a digit in the end to make the number equal to the length of the remaining string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.