51NOD 1264 两线段相交模板

1264 线段相交

给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交)。 如果相交,输出"Yes",否则输出"No"。
Input
第1行:一个数T,表示输入的测试数量(1 <= T <= 1000)
第2 - T + 1行:每行8个数,x1,y1,x2,y2,x3,y3,x4,y4。(-10^8 <= xi, yi <= 10^8)
(直线1的两个端点为x1,y1 | x2, y2,直线2的两个端点为x3,y3 | x4, y4)
Output
输出共T行,如果相交输出"Yes",否则输出"No"。
Input示例
2
1 2 2 1 0 0 2 2
-1 1 1 1 0 0 1 -1
Output示例
Yes
No

 1 //(a-c)×(d-c)*(d-c)×(b-c)>=0&&(c-a)×(b-a)*(b-a)×(d-a)>= 0就可以判断ab,cd相交
 2 #include <iostream>
 3 using namespace std;
 4 struct node{
 5     double x,y;
 6 };
 7 bool cj(node a,node b,node c,node d){
 8     double A=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
 9     double B=(d.x-c.x)*(b.y-c.y)-(b.x-c.x)*(d.y-c.y);
10     if(A*B<0){
11         return false;
12     }
13     return true;
14 }
15 bool check(node a,node b,node c,node d){
16     if(!cj(a,b,c,d)){
17         return false;
18     }
19     if(!cj(c,d,a,b)){
20         return false;
21     }
22     return true;
23 }
24 int main(){
25     cin.sync_with_stdio(false);
26     int T;
27     node a,b,c,d;
28     cin>>T;
29     while(T--){
30         cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
31         if(check(a,b,c,d)){
32             cout<<"Yes"<<endl;
33         }
34         else{
35             cout<<"No"<<endl;
36         }
37     }
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/xjh-shin/articles/6137138.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值