51Nod 1264: intersects the line segment (Computational Geometry)

51Nod 1264: segment intersection

Decision

Two end points are given two line segments on a plane, it is determined whether two line segments intersect (there is a common point that intersect or partially overlap). If the intersection, output "Yes", otherwise a "No".

Input

Line 1: a number T, indicates the number of test inputs (1 <= T <= 1000 )
of 2 - T + 1 rows: 8 per row number, x1, y1, x2, y2 , x3, y3, x4, y4. (. 8 ^ -10 <= XI, Yi <^ = 10. 8)
(two points of the line 1 is x1, y1 | x2, y2, two points of the line 2 is x3, y3 | x4, y4)

Output

The output common-T lines intersect the output if "Yes", otherwise a "No".

SAMPLE INPUT

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

Sample Output

Yes
No

Solve

Straddles the experiment
if two line segments intersect, the two segments necessarily mutually straddling each other. If the straddle P1P2 of Q1Q2, the vector (P1 - Q1) and (P2 - Q1) is located in the vector (Q2 - Q1) on both sides, namely (P1 - Q1) × (Q2 - Q1) * (P2 - Q1) × ( Q2 - Q1) <0. It can be rewritten as a formula (P1 - Q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> 0. When (P1 - Q1) × (Q2 - Q1) = 0 , it indicates that (P1 - Q1) and (Q2 - Q1) collinear, but as it has been by flash exclusion test, so the line segment P1 certain of Q1Q2; Similarly, (Q2 - Q1) × (P2 - Q1) = 0 must be described on the line segment P2 Q1Q2. It is judged based Q1Q2 straddling P1P2 is :( P1 - Q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> = 0. Similarly Q1Q2 straddling determination is based P1P2 :( Q1 - P1) × (P2 - P1) * (P2 - P1) × (Q2 - P1)> = 0. Situation as shown below:
Geometry_2.gif
(taken from above [ https://dev.gameres.com/Program/Abstract/Geometry.htm#%E5%88%A4%E6%96%AD%E4%B8%A4%E7% the BF E6%%% BA% B5% the AE E6 E5% 98% 90%%% the AF A6 B8% E7% 9B% E4% BA% A4% ])

Code

/*************************************************************************

     > Author: WZY
     > School: HPU
     > Created Time:   2019-06-25 17:43:54
     
************************************************************************/
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=(1<<30);
const ll INF=(1LL*1<<60);
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
struct node
{
    double x,y;
}p[maxn];
double cel(node a,node b,node c)
{
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<4;i++)
            cin>>p[i].x>>p[i].y;
        if(cel(p[0],p[1],p[2])*cel(p[0],p[1],p[3])<=0&&cel(p[2],p[3],p[0])*cel(p[2],p[3],p[1])<=0)
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Friends-A/p/11085384.html