Segments_poj3304_计算几何

本文探讨了一道关于二维空间中线段投影的问题:如何判断是否存在一条直线,使得所有线段在其上的投影至少有一点交集。通过分析问题特性,提出了解决方案,并提供了具体的算法实现。

Description


Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input


Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.

Output


For each test case, your program must output “Yes!”, if a line with desired property exists and must output “No!” otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

Analysis


问题可以转换成求是否存在一条直线能穿过所有给出的线段
不难想到这条直线在一定范围内平移旋转仍能保持其原性质(穿过所有的线段),于是可以将直线平移旋转,使其穿过某些线段的端点
∵两点确定一条直线
∴我们可以枚举两个端点作直线,再枚举线段利用叉积判断是否被直线穿过
poj的pascal编译莫名爆了,可怜某媳妇一秒钟,c++大法好

Code


#include <stdio.h>
using namespace std;
struct point
{
    double x,y;
}p[201];
double cros(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        bool flag,ans=false;
        int cnt=0;
        for (int i=1;i<=n;i++)
        {
            ++cnt;scanf("%lf%lf",&p[cnt].x,&p[cnt].y);
            ++cnt;scanf("%lf%lf",&p[cnt].x,&p[cnt].y);
        }
        for (int i=1;i<=cnt-1&&!ans;i++)
            for (int j=i+1;j<=cnt&&!ans;j++)
            {
                if (p[i].x==p[j].x&&p[i].y==p[j].y)
                    continue;
                flag=false;
                for (int k=1;k<=cnt;k+=2)
                    if (cros(p[i],p[j],p[k])*cros(p[i],p[j],p[k+1])>0)
                    {
                        flag=true;
                        break;
                    }
                if (!flag)
                {
                    ans=true;
                    printf("Yes!\n");
                }
            }
        if (!ans)
            printf("No!\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值