Missing Vertex In Parallelogram

Easy
0/40
Average time to solve is 10m
profile
Contributed by
6 upvotes
Asked in companies
Morgan StanleyMAQ Software

Problem statement

Euclid, Pythagoras, Pascal and Monte plan to play in a park. Pascal, Monte and Euclid enter the park first and stand at three different arbitrary positions. As Pythagoras is the last one to reach the park, he decides to stand in such a way that the four points form a parallelogram if joined with each other. The positions of Euclid and Monte form one of the two diagonals of the parallelogram.

Your task is to help Pythagoras determine the coordinates of the point where he must stand so that a parallelogram is formed.

A parallelogram is a simple quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure

For example, consider the scenario shown below. The coordinates of Euclid, Pascal, and Monte have been shown in the figure. 

alt text

Euclid and Monte are standing at the endpoints of a diagonal and Pascal is standing at one vertex of the other diagonal. Using the coordinates of Pascal, Euclid and Monte, figure out the coordinates of Pythagoras, which will be (10, 15).
Note:
The coordinates of Pythagoras will be unique for the unique values of the other three coordinates.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains a single integer T, representing the number of test cases.

The first and only line of each test case contains six space-separated integers, x1, y1, x2, y2, x3, y3.

x1→ X coordinate of Euclid
y1→ Y coordinate of Euclid
x2→ X coordinate of Pascal
y2→ Y coordinate of Pascal
x3→ X coordinate of Monte
y3→ Y coordinate of Monte
Output Format:
For each test case, print two space-separated integers, denoting the values of X and Y coordinates of Pythagoras.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function. 
Return a list containing two integers denoting the x and the y-coordinate of Pythagoras.
Constraints:
1 <= T <= 10^2
-10^9 <= x1, y1, x2, y2, x3, y3 <= 10^9

Time Limit: 1sec
Sample Input 1:
1
5 5 15 5 20 15
5 5 5 10 20 10
Sample Output 1:
10 15
20 5
Explanation For Sample Input 1:
The first test case is the same input as shown in the diagram above. As is clear from the image, the coordinates of Pythagoras are (10, 15).

The second test case has two coordinates having the value of x as 5 so the other two would be having the same x value and similarly for y value. Thus, the coordinates are (20, 5).
Sample Input 2:
1
2 2 4 2 4 7 
Sample Output 2:
2 7 
Explanation For Sample Input 2:
In the first test case, the fourth coordinate will be on (2, 7).
Hint

Can you use coordinate geometry to find the missing coordinates?

Approaches (1)
Use Coordinate Geometry

Approach: We know that the diagonals of a parallelogram bisect each other. That means the point where two diagonals (AC and BD) of a parallelogram(ABCD) intersect divides AC and BD into two equal halves. See the image below for more clarity:-

In the above parallelogram, E is the point of intersection of AC and BD. Hence, E divides both Ac and BD into two equal halves. As is clear, E is the midpoint of both AC and BD. Hence, given the coordinates of A, B, and C, we can find the coordinates of D using the formula for the midpoint of a line segment joining two points.

 

For any two arbitrary points A(x1, y1) and B(x2, y2), the midpoint of the line segment joining AB is - {(x1+x2)/1, (y1+y2)/2}. 

 

Using the above formula on our problem, 
(Ax + Cx)/2 = (Bx + Dx)/2 and,

(Ay + Cy)/2 = (By + Dy)/2 where

A→ Euclid, B→ Pascal, C→ Monte, D→ Pythagoras.

Solving for Dx and Dy, we get the following relations:-

 

Dx = Ax + Cx - Bx and Dy = Ay + Cy - By.

 

We can then use these relations directly to find out the coordinates of missing point D (Pythagoras in our case).

Time Complexity

O(1).

 

As all operations will be performed in constant time.

Space Complexity

O(1).

 

As we are using constant extra memory.

Code Solution
(100% EXP penalty)
Missing Vertex In Parallelogram
Full screen
Console