1041-线段相交
描述:
判断给定线段L1,L2是否相交,其中L1表示为s1x,s1y,e1x,e1y,L2表示为s2x,s2y,e2x,e2y
输入:
多组数据输入,每组一行,每组八个浮点数,s1x,s1y,e1x,e1y,s2x,s2y,e2x,e2y
输出:
相交则输出yes否则输出no
样例输入:
0 1 2 1 1 0 1 2
1 1 2 2 3 3 4 4
样例输出:
yes
no
1 #include<iostream> 2 using namespace std; 3 4 struct Point 5 { 6 double x; 7 double y; 8 }; 9 10 struct Segment 11 { 12 Point p; 13 Point q; 14 }; 15 16 struct Segment seg[3]; 17 18 bool IsCross(struct Segment segment1,struct Segment segment2) 19 { 20 Point d1,d2,d3; 21 d1.x=segment2.q.x-segment1.p.x; 22 d1.y=segment2.q.y-segment1.p.y; 23 24 d2.x=segment2.p.x-segment1.p.x; 25 d2.y=segment2.p.y-segment1.p.y; 26 27 d3.x=segment1.q.x-segment1.p.x; 28 d3.y=segment1.q.y-segment1.p.y; 29 30 if((d1.x*d3.y-d1.y*d3.x)*(d3.x*d2.y-d3.y*d2.x)>0) 31 return true; 32 else 33 return false; 34 } 35 36 int main() 37 { 38 while(cin>>seg[1].p.x>>seg[1].p.y>>seg[1].q.x>>seg[1].q.y>>seg[2].p.x>>seg[2].p.y>>seg[2].q.x>>seg[2].q.y) 39 { 40 if(IsCross(seg[1],seg[2])&&IsCross(seg[2],seg[1])) 41 cout<<"yes"<<endl; 42 else 43 cout<<"no"<<endl; 44 } 45 return 0; 46 }