51nod 1264 线段相交

题目:传送门

题意:给两条线段,有一个公共点或有部分重合认为相交,问他们是否相交。

题解:这属于非规范相交的情况,模板题。

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
struct point{
    double x,y;
}a,b,c,d;
const double eps=1e-3; //精度问题
double cross(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double Across(point a,point b,point c,point d) //非规范相交
{
    double tmp=cross(c,b,a)*cross(d,b,a);
    double tmp2=cross(a,c,d)*cross(b,c,d);
    if((tmp<=0||fabs(tmp)<eps)&&(tmp2<=0||fabs(tmp2)<eps)) return true;
    return false;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
        if(Across(a,b,c,d))
        puts("Yes");
        else
        puts("No");
    }
    return 0;
}

 

posted @ 2016-08-07 19:48  Ritchie丶  阅读(271)  评论(0)    收藏  举报