51Nod 1264 线段相交

1264 线段相交

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注

描述

给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交)。 如果相交,输出"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

题解

通过判断叉积来判断线段是否相交。代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long

using namespace std ;

struct point {double x , y ;} ;

double xmul ( point a , point b , point c ){
    return ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x ) ;
}

int main(){
    int t ;
    cin >> t ;
    while ( t -- ){
        point a , b , c , d ;
        cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y >> d.x >> d.y ;
        if ( xmul(a , b , c) * xmul(a , b , d) <= 0 && xmul(c , d , a) * xmul(c , d , b) <= 0 ){
            cout << "Yes" << endl ;
        }else{
            cout << "No" << endl ;
        }
    }
    return 0 ;
}

转载于:https://www.cnblogs.com/Cantredo/p/9823373.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值