• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
magicat
博客园    首页    新随笔    联系   管理    订阅  订阅
POJ3304

枚举所有线段交点,依次判断是否与所有线段都有交点

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

#define x first
#define y second

using namespace std;

typedef pair<double, double> PDD;
const int N = 210;
const double eps = 1e-8;

int n;
PDD q[N], a[N], b[N];


int sign(double x)
{
    if (fabs(x) < eps) return 0;
    if (x < 0) return -1;
    return 1;
}


int cmp(double x, double y)
{
    if (fabs(x - y) < eps) return 0;
    if (x < y) return -1;
    return 1;
}


double cross(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}

double area(PDD a, PDD b, PDD c)
{
    return cross(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y);
}


bool check()
{
    for(int i=0;i<n*2;i++)
    {
        for(int j=i+1;j<n*2;j++)
        {
            if(!cmp(q[i].x,q[j].x)&&!cmp(q[i].y,q[j].y))    continue;
            bool flag = true;
            for(int k=0;k<n;k++)
            {
                if(sign(area(q[i],q[j],a[k]))*sign(area(q[i],q[j],b[k]))>0)
                {
                    flag=false;
                    break;
                }
            }
            if(flag)    return true;
        }
    }
    return false;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0,k=0;i<n;i++)
        {
            double x1,y1,x2,y2;
            cin>>x1>>y1>>x2>>y2;
            q[k++]={x1,y1};
            q[k++]={x2,y2};
            a[i]={x1,y1},b[i]={x2,y2};
        }
        if(check()) cout<<"Yes!"<<endl;
        else cout<<"No!"<<endl;
    }
    return 0;
}

本文来自博客园,作者:magicat,转载请注明原文链接:https://www.cnblogs.com/magicat/p/15559024.html

posted on 2021-11-15 23:00  magicat  阅读(41)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3