1. 程式人生 > >51Nod 1264 線段相交

51Nod 1264 線段相交

point .com -c color http pre img 公式 truct

技術分享

/*  (a-c)×(d-c)*(d-c)×(b-c)>=0&&(c-a)×(b-a)*(b-a)×(d-a)>= 0就可以判斷ab,cd相交*/
/*  p1×p2 = x1y2 - x2y1 = - p2×p1-----(叉乘公式)叉積*/
#include<stdio.h>
struct point
{
    double x,y;
};
//struct point a,b,c,d;
int mp(point a,point b,point c,point d){
    double C=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
    
double D=(d.x-c.x)*(b.y-c.y)-(b.x-c.x)*(d.y-c.y); if(C*D<0) return 0; else return 1; } bool check(point a , point b , point c , point d){ if(!mp(a,b,c,d)) return false; if(!mp(c,d,a,b)) return false; return true; } int main(){ int T; scanf("%d",&T); while(T--){ point a,b,c,d; scanf(
"%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y); if(check(a,b,c,d)) printf("Yes\n"); else printf("No\n"); } }

51Nod 1264 線段相交