


Ninja has been given 2 points ‘A’ and ’B’ that corresponds to the line ‘AB’ and ‘P’, ’Q’ that corresponds to line ‘PQ’ on a 2D plane. Ninja wants to find the intersection point of the ‘AB’ and ‘PQ’ lines up to 6 decimal places. If there is no such intersection point print -1.000000 -1.000000.
Note:
1. You do not need to fix the output up to 6 decimal places. it has already been taken care of. Just return the output in the data type mentioned in the function.
2. Lines ‘AB’ and ‘PQ’ are two different lines.
For Example: For A(0, 3), B(3, 0) and P(0, 0), Q(5, 5) the point of intersection of line ‘AB’ and ‘PQ’ is (1.500000, 1.500000). As shown below:
As Ninja is weak in Geometry, can you help him to find the intersection of these two lines?
The first line of input contains an integer ’T’ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains 8 single space-separated integers ‘AX1’,’ AY1’, ’BX2’, ’BY2’, ’PX1’, ’PY1’, ’QX2’, ’QY2’ where ’AX1’ represents the ‘X’ coordinate of the point ‘A’ and ‘AY1’ represents the ‘Y’ coordinate of the point ‘A’ and so on.
Output Format :
For each test case, print the ‘X’ and ‘Y’ coordinates of the point of intersection of the two lines ‘AB’ and ‘PQ’ up to 6 decimal places.
Print the output of each test case in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10^2
1 <= ‘N’ <= 5*10^3
1 <= ‘AX1’, ’AY1’, ’BX2’, ’BY2’, ’PX1’, ’PY1’, ’QX2’, ’QY2’ <= 10^5
Where ‘T’ denotes the total number of test cases, ‘N’ represents the number of boxes, ‘AX1’, ’AY1’, ’BX2’, ’BY2’, ’PX1’, ’PY1’, ’QX2’, ’QY2’ represents the the ‘X’ coordinate of the point ‘A’ and ‘AY1’ represents the ‘Y’ coordinate of the point ‘A’ and so on.
Time Limit: 1 sec
2
1 1 3 3 0 0 -1 1
2 3 4 5 2 6 1 7
0.000000 0.000000
3.500000 4.500000
For sample test case 1:
The point of intersection of line ‘AB’ and ‘PQ’ is (0, 0) and the output is printed up to 6 decimal places. See the figure below:
For sample test case 2:
The point of intersection of lines ‘AB’ and ‘PQ’ is (3.5, 4.5) and the output is printed up to 6 decimal places. See the figure below:
2
0 1 2 1 0 -1 4 -1
1 1 2 2 0 1 2 1
-1.000000 -1.000000
1.000000 1.000000
For sample test case 1:
The line ‘AB’ and ‘PQ’ are parallel to each other so there is no point of the intersection so return (-1, -1). See the figure below:
For sample test case 2:
The point of intersection of line ‘AB’ and ‘PQ’ is (1, 1). See the figure below:
Try to find out the intersection point of ‘AB’ and ‘PQ’ using the equation of both lines.
The idea behind this approach is to derive the equation of both of the lines ‘AB’ and ‘PQ’ and check if the Slope of these two lines is equal or not. Following are the two cases:
For a better understanding of this approach, Assume that we have two points which are A(a1, b1) and B(a2, b2). Now, we have to find the equation of line formed by points ‘A’ and ‘B’.
Now we have to solve these 2 equations to get the point of intersection of these two lines. To solve this problem, we can multiply equation 1 by b2 and equation 2 by b1,this gives us the following result:
Here is the complete algorithm:
O(1).
Because we are computation results from the given coordinates using binary operators. Hence overall time complexity will be O(1).
O(1).
Because we are not using any extra space. Hence the overall space complexity will be O(1).