Segments POJ - 3304 计算几何

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.

Sample Input
3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0
Sample Output
Yes!
Yes!
No!

问题可以转化为 给你n个线段,问你有没有一条直线穿过他们。如果有,那么这条直线的垂线就是所求直线。

一开始想的是直接找两条最短的线段,以他们的端点求四个向量,如果存在这样一条直线,一定与这四个向量中的某一条共线。

然而然而。。。。。。。

我不知道他给的线段是不是没有重复点,如果有,你会求出一个零向量,然后你叉乘的时候都是0,所以都符合,就错了。

其次,他的点 如果靠的很近,那么就认为是一个点,所以我的这种做法判断的更加严格,可能会把本来yes的答案判定为no。

然后我就没有好办法了,然后就枚举所有端点了。代码是抄的,还是贴出来吧。


#include <iostream>
#include <math.h>
#include<cstdio>
using namespace std;
#define MAXM 110
#define EPS 1e-8
typedef struct
{
    double x1,y1,x2,y2;
} Segment;
Segment segment[MAXM];
int n;
double distance(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double corss(double x1,double y1,double x2,double y2,double x,double y)
{
    return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
}

bool judge(double x1,double y1,double x2,double y2)
{
    int i;
    if(distance(x1,y1,x2,y2)<EPS) return 0;
    for(i=0; i<n; i++)
    {
        if(corss(x1,y1,x2,y2,segment[i].x1,segment[i].y1)*
                corss(x1,y1,x2,y2,segment[i].x2,segment[i].y2)>EPS) return 0;
    }
    return 1;
}

int main()
{
    int t,i,j,flag;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0; i<n; i++)
            scanf("%lf%lf%lf%lf",&segment[i].x1,&segment[i].y1,&segment[i].x2,&segment[i].y2);
        if(n==1)
        {
            printf("Yes!\n");
            continue;
        }
        flag=0;
        for(i=0; i<n && !flag; i++)
            for(j=i+1; j<n && !flag; j++)
            {
                if(judge(segment[i].x1,segment[i].y1,segment[j].x1,segment[j].y1) ||
                        judge(segment[i].x1,segment[i].y1,segment[j].x2,segment[j].y2) ||
                        judge(segment[i].x2,segment[i].y2,segment[j].x1,segment[j].y1) ||
                        judge(segment[i].x2,segment[i].y2,segment[j].x2,segment[j].y2))
                    flag=1;
            }
        if(flag) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值