POJ 3304 Segments(在直线上投影有公共点,直线和线段交)

Segments
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 14289
Accepted: 4556

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!

Source


题目大意:
   给出n条线段,判断是否存在有一条直线,满足所有的线段在直线上投影后至少有一个公共点。
枚举所有线段的端点中的任意两点所在的直线是否与所有线段相交就好了。
以下是判断直线和线段相交的代码
bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
{
    return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0;
}
以下是AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
	if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    //绕原点旋转角度B(弧度值),后x,y的变化
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
//只有第一个值为2时,交点才有意义
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
    if(sgn((s-e)^(b.s-b.e)) == 0)
    {
        if(sgn((s-b.e)^(b.s-b.e)) == 0)
            return make_pair(0,res);//重合
        else return make_pair(1,res);//平行
    }
    double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
    res.x += (e.x-s.x)*t;
    res.y += (e.y-s.y)*t;
    return make_pair(2,res);
    }
}line[105];
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
const int MAXN = 1010;
Point list[MAXN];
int Stack[MAXN],top;
//相对于list[0]的极角排序
bool _cmp(Point p1,Point p2)
{
    double tmp = (p1-list[0])^(p2-list[0]);
    if(sgn(tmp) > 0)return true;
    else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0)
    return true;
    else return false;
}
bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
{
    return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0;
}
bool check(Line l1,int n)
{
    if(sgn(dist(l1.s,l1.e)) == 0 )return false;
    for(int i = 0;i < n;i++)
        if(Seg_inter_line(l1,line[i]) == false)
            return false;
    return true;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%lf%lf%lf%lf",&line[i].s.x,&line[i].s.y,&line[i].e.x,&line[i].e.y);
		}
		bool flag=false;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(check(Line(line[i].s,line[j].s),n) || check(Line(line[i].s,line[j].e),n)
                        || check(Line(line[i].e,line[j].s),n) || check(Line(line[i].e,line[j].e),n) )
                {
                    flag = true;
                    break;
                }
			}
		}
		if(flag)printf("Yes!\n");
		else printf("No!\n");
	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值