POJ-3304 Segments(计算几何)

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13113 Accepted: 4191

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 (x1y1) and (x2y2) 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!


  这个当作用点判断线段是否相交的模板吧,和以前用的叉积函数也不一样了,是三个参数

/*
POJ-3304  Segments
给定一些线段,问是否有直线L存在,使得
所有线段到 L的投影有至少一个公共点。

实际上就是问,是否存在一条直线K, 和所
有这些线段都相交。 L是K的垂线即可。

若存在一条直线与所有线段相交,该直线必
定经过这些线段的某两个端点(否则可以平
移或转动使之靠上端点)。枚举任意两个端
点构造直线并看它是否与每条线段相交即可。
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const double eps = 1e-8;

struct Point
{
    double x,y;
    Point(double xx=0,double yy=0):x(xx),y(yy){}
};
typedef Point Vector;
//#define Vector Point

int n;
Point ps[205];
Vector operator - (Point b, Point a)
{
    return Vector(b.x - a.x, b.y - a.y);
}


int dcmp(double x)//三态函数
{
    if(fabs(x) < eps)
        return 0;
    else
        return x < 0 ? -1 : 1;
}

bool operator == (const Point & a,const Point & b)
{
    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}

//点乘
double Dot(const Vector& A, const Vector& B)
{
	return A.x*B.x + A.y*B.y;
}

double cross(const Point& o,const Point& a,const Point &b)
{
    return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}

//判断直线ab是否与线段cd相交
//0 不相交
//1 规范相交
//2 不规范相交
int segLineCross(const Point& a,const Point& b,const Point& c,const Point& d)
{
    int d1,d2;
    d1 = dcmp(cross(a,b,c));//模板抄错了,忘了sig函数

    d2 = dcmp(cross(a,b,d));//同上
    if((d1^d2)==-2)//注意加小括号,^的优先级比==低
        return 1;
    if(d1==0||d2 == 0)
        return 2;
    return 0;
}

bool Judge(const Point a,const Point b)
{
    if(a == b)
        return false;//判断点是否重复
    for(int i = 1;i < n*2;i += 2)//枚举所有线段,看是否都相交
        if(!segLineCross(a,b,ps[i],ps[i+1]))
            return false;
    return true;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 1;i <= n*2;i++)
            scanf("%lf%lf",&ps[i].x,&ps[i].y);

        bool flag = false;
        for(int i = 1;i < n*2;i++)//枚举任意两端点
            for(int j = i+1;j <= n*2;j++)
                if(Judge(ps[i],ps[j]))
                {
                    flag = true;
                    break;
                }

        if(flag)
            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、付费专栏及课程。

余额充值