判断线段相交

参考资料

http://www.cnblogs.com/zhangchaoyang/articles/2668562.html


代码

#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct node
{
    int x;
    int y;
} NODE;
int cross_products(NODE a,NODE b,NODE c)
{
    int x1=b.x-a.x;
    int y1=b.y-a.y;
    int x2=c.x-a.x;
    int y2=c.y-a.y;
    return x1*y2-x2*y1;
}
bool on_segment(NODE a,NODE b,NODE c)
{
    if((c.x>=a.x && c.x<=b.x || c.x>=b.x && c.x<=a.x) && (c.y>=a.y && c.y<=b.y || c.y>=b.y && c.y<=a.y))
        return true;
    return false;
}
bool segment_intersection(NODE p1,NODE p2,NODE p3,NODE p4)
{
    int d1=cross_products(p3,p4,p1);
    int d2=cross_products(p3,p4,p2);
    int d3=cross_products(p1,p2,p3);
    int d4=cross_products(p1,p2,p4);
    if(d1*d2<0&& d3*d4<0)
        return true;
    else if(d1==0 && on_segment(p3,p4,p1))
        return true;
    else if(d2==0 && on_segment(p3,p4,p2))
        return true;
    else if(d3==0 && on_segment(p1,p2,p3))
        return true;
    else if(d4==0 && on_segment(p1,p2,p4))
        return true;
    return false;
}
int main()
{
    NODE p[5];
    while(1)
    {
        for(int i=1; i<=4; i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        if(segment_intersection(p[1],p[2],p[3],p[4]))
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值