【poj3304】计算几何 线段与直线的投影

Segments

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19350 Accepted: 6082

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 xyxy2 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!

题目原意是求是否有一条直线,使得全部给出的线段投影到直线上,至少有一个公共点

首先解释下投影,一条线段投影到一条直线上,如下图所示

线段AB投影到直线l上就是线段CD的样子

我们现在需要找到这样一条直线,使得所有线段投影都都至少交于同一个公共点

我们首先枚举所有线段的两个端点,如果两个端点组成的直线经过所有的线段,那么这条直线的垂线就是我们所要找到的答案

例如样例2,已知有三条绿色的已知线段AB、CD、DE,这时我们枚举所有端点,发现端点A和D组成的直线M经过所有的线段,那么做直线M的任一垂线ANS就是最后的答案

嗯?你问我为什么?你可以倒过来看看

这时候所有绿色的线段做ANS的投影时,都交于至少同一个公共点,就是黄线M和红线ANS的交点

这也就是为什么我们一开始的时候,枚举所有的端点之后判断是否存在一条直线经过所有的线段

以下是代码,代码来自kuangbin聚聚

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;

const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0) return -1;
    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;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
};
double xmult(Point p0,Point p1,Point p2) //p0p1 X p0p2
{
    return (p1-p0)^(p2-p0);
}
bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
{
    return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= 0;
}
double dist(Point a,Point b)
{
    return sqrt( (b - a)*(b - a) );
}
const int MAXN = 110;
Line line[MAXN];
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 n;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        double x1,y1,x2,y2;
        for(int i = 0; i < n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[i] = Line(Point(x1,y1),Point(x2,y2));
        }
        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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值