51NOD 1264 线段相交

来源:互联网 发布:手机店铺销售软件 编辑:程序博客网 时间:2024/05/16 08:33

就是判断两条线段是否相交。

要么判断MBR 要么判断跨立。

计算叉积的乘积。判断跨立。叉积的几何意义,参见matrix67大神的博客。

#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>//#include<list>//#include<set>//#include<bitset>#include<vector>//#include<valarray>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,b) for(int i=a;i<b;i++)#define REP(i,a,b) for(int i=a;i>=b;i--)#define sf scanf#define pf printf#define all(v) (v).begin(),(v).end()#define acfun std::ios::sync_with_stdio(false)#define DEBUG freopen("in.txt","r",stdin);\freopen("out.txt","w",stdout);#define SIZE (1000 +2)#define MOD 1000000007using namespace std;struct point{    double x,y;    point(double xx=0,double yy=0){x=xx,y=yy;}};inline point getvector(point a,point b){    point tmp(b.x-a.x,b.y-a.y);    return tmp;}inline double crossproduct(point a,point b){    return a.x*b.y-a.y*b.x;}bool solve(point a,point b,point c,point d){    point ca,cb,cd,ac,ad,ab;    ca=getvector(c,a);    cb=getvector(c,b);    cd=getvector(c,d);    ac=getvector(a,c);    ad=getvector(a,d);    ab=getvector(a,b);    if(crossproduct(cd,ca)*crossproduct(cd,cb)<=0&&       crossproduct(ac,ab)*crossproduct(ad,ab)<=0)        return 1;    else        return 0;}int main(){    int t;    sf("%d",&t);    while(t--)    {        point l[4];        FOR(i,0,4)        sf("%lf%lf",&l[i].x,&l[i].y);        if(solve(l[0],l[1],l[2],l[3]))            puts("Yes");        else            puts("No");    }}


0 0
原创粉丝点击