Problem8549--CGL_2_C : Cross Point

8549: CGL_2_C : Cross Point

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 256 MiB  Special Judge

Description

For given two segments $s_1$ and $s_2$, print the coordinate of the cross point of them.
$s_1$ is formed by end points $p_0$ and $p_1$, and $s_2$ is formed by end points $p_2$ and $p_3$.

Input

The entire input looks like:
q(the number of queries) 
1st query 
2nd query 
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
$x_{p_0}\ y_{p_0}\ x_{p_1}\ y_{p_1}\ x_{p_2}\ y_{p_2}\ x_{p_3}\ y_{p_3}$

Output

For each query, print the coordinate of the cross point.
The output values should be in a decimal fraction with an error less than 0.00000001.

Constraints

$1 ≤ q ≤ 1000$
$-10000 ≤ x_{p_i}, y_{p_i} ≤ 10000$
$p_0≠p_1$ and $p_2≠p_3$.

Sample 1 Input

3
0 0 2 0 1 1 1 -1
0 0 1 1 0 1 1 0
0 0 1 1 1 0 0 1

Sample 1 Output

1.0000000000 0.0000000000
0.5000000000 0.5000000000
0.5000000000 0.5000000000

HINT

相同题目:CGL_2_C

【讲解】

两条线段 $s_1,s_2$ 的交点坐标可以通过外积的大小求得。

如图所示,用向量 $s_2.p_2-s2.p_1=\text{base}$ 来表示线段 $s_2$,分别求通过 $s_2.p_1$ 和 $s_2.p_2$ 的直线与线段 $s_1$ 两个端点的距离 $d_1,d_2$。
设 $s_1.p_1-s_2.p_1$ 为向量 $\text{hypo}$,那么 $\text{base}$ 和 $\text{hypo}$ 构成的平行四边形面积就是外积 $\text{base} \times \text{hypo}$ 的大小。只要用面积除以 $\text{base}$ 的大小即可求出 $d_1$。
$d_1=\frac{\text{base} \times \text{hypo}}{|\text{base}|}=\frac{\text{base} \times (s_1.p_1-s_2.p_1)}{|\text{base}|}$。
同理,$d_2==\frac{\text{base} \times (s_1.p_2-s_2.p_1)}{|\text{base}|}$。
然后,设线段 $s_1$ 的长度与点 $s_1.p_1$ 到交点 $x$ 的距离之比为 $t$,则有 $d_1:d_2=t:(1-t)$。
由此可得 $t=d_1/(d_1+d_2)$。
所以交点 $x=s_1.p_1+(s_1.p_2-s_1.p_1) \times t$。

Source/Category