51nod 1264 线段相交——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1264
检查点的位置就行了,具体见注释。
1 /* (a-c)×(d-c)*(d-c)×(b-c)>=0检查c点在ab之间 2 &&(c-a)×(b-a)*(b-a)×(d-a)>= 0检查a点在cd之间 3 就可以判断ab,cd相交(可以画个图看看) 4 (x1,y1)×(x2,y2) = x1y2 - x2y1 (向量积)*/ 5 #include <iostream> 6 #include <cstdio> 7 using namespace std; 8 struct point{ double x,y; };; 9 10 bool cross(point a,point b,point c,point d){//检查c点在ab之间 11 double p=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);//CA×CD 12 double q=(d.x-c.x)*(b.y-c.y)-(b.x-c.x)*(d.y-c.y);//CD×CB 13 if(p*q<0) return false; 14 else return true; 15 } 16 int main(){ 17 18 int t; scanf("%d",&t); 19 while(t--){ 20 point a,b,c,d; 21 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); 22 if(cross(a,b,c,d)&&cross(c,d,a,b)) 23 puts("Yes"); 24 else 25 puts("No"); 26 } 27 }
Stay Hungry, Stay Foolish