Given three coordinate points A, B and C, find the missing point D such that ABCD can be a parallelogram.
Input : A = (1, 0) B = (1, 1) C = (0, 1) Output : 0, 0 Explanation: The three input points form a unit square with the point (0, 0) Input : A = (5, 0) B = (1, 1) C = (2, 5) Output : 6, 4
As shown in below diagram, there can be multiple possible outputs, we need to print any one of them.
A quadrilateral is said to be a parallelogram if its opposite sides are parallel and equal in length.
As we’re given three points of the parallelogram, we can find the slope of the missing sides as well as their lengths.
The algorithm can be explained as follows
Let R be the missing point. Now from definition, we have
- Length of PR = Length of QS = L1 (Opposite sides are equal)
- Slope of PR = Slope of QS = M1 (Opposite sides are parallel)
- Length of PQ = Length of RS = L2 (Opposite sides are equal)
- Slope of PQ= Slope of RS = M2 (Opposite sides are parallel)
Thus we can find the points at a distance L1 from P having slope M1 as mentioned in below article :
Find points at a given distance on a line of given slope.
Now one of the points will satisfy the above conditions which can easily be checked (using either condition 3 or 4)
Below is the implementation of the above approach:
0, 0 6, 4
Since the opposite sides are equal, AD = BC and AB = CD, we can calculate the co-ordinates of the missing point (D) as:
AD = BC (Dx - Ax, Dy - Ay) = (Cx - Bx, Cy - By) Dx = Ax + Cx - Bx Dy = Ay + Cy - By
Below is the implementation of above approach:
This article is contributed by Ashutosh Kumar 😀 and Abhishek Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes