Problem8548--CGL_2_B : Intersection

8548: CGL_2_B : Intersection

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

Description

For given two segments $s_1$ and $s_2$, print "1" if they are intersect, "0" otherwise.
$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 "1" or "0".

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 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0

Sample 1 Output

1
1
0

HINT

相同题目:CGL_2_B

【讲解】


如上图 (a)(b)(c)(d) 是两条线段相交的例子,(b) 中一条线段的端点位于另外一条线段上,(c) 中两条线段共用一个端点,(d)中两条线段平行重合,这些情况均为相交。(e)(f) 则是两条线段不相交的例子。
利用逆时针方向检查点线之间的位置关系,我们就能轻松判断两条线段是否相交。
分别检查两条线段,如果双方都符合 “另一条线段的两个端点分别位于当前线段的顺时针和逆时针方向”,则两条线段相交。如下图所示。

Source/Category