POJ 3304 直线与线段相交的判断

参考:
http://blog.sina.com.cn/s/blog_6635898a0100n2lv.html
的证明过程
可将问题转化为是否存在一条直线经过所有的线段的判断问题

#include<iostream>
#include<cmath>
#include<cstdio> 
using namespace std;

const int MAX = 105;
const double eps = 1e-8;
struct Point{
    double x,y;
}s[MAX],e[MAX];
int n;

double mult(Point sp,Point ep,Point op){
    return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}

bool solve(Point p1,Point p2){
    if(abs(p1.x-p2.x)<eps && abs(p1.y-p2.y)<eps) return false;
    for(int i=0;i<n;i++)
        if(mult(p1,p2,s[i])*mult(p1,p2,e[i])>eps)   return false;
    return true;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>s[i].x>>s[i].y>>e[i].x>>e[i].y;

        bool flag=false;
        if(n<3) flag=true;

        for(int i=0; i<n && !flag ;i++){
            for(int j=i+1; j<n && !flag ;j++){
                if(solve(s[i],s[j])) flag=true;
                else if(solve(s[i],e[j])) flag=true;
                else if(solve(e[i],s[j])) flag=true;
                else if(solve(e[i],e[j])) flag=true;
            }
        }
        if(flag)    cout<<"Yes!"<<endl;
        else        cout<<"No!"<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值