poj 3304 Segments(叉积+直线和线段相交判断)

该博客探讨了一个编程问题,即给定n个二维空间的线段,判断是否存在一条直线,使得所有线段在该直线上的投影至少有一个公共点。通过分析,将问题转换为寻找一条与所有线段相交的直线。采用叉积的方法来判断线段与直线的相交情况,并在代码实现中处理特殊情况,如重合的点。样例输入和输出展示了不同情况下的结果。
摘要由CSDN通过智能技术生成

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.

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个线段,问是否存在一条直线,使得这n条线段在该直线上的投影存在至少一个公共点?

思路:假设存在,那么从该公共点出发做一条与该直线垂直的直线,这条直线必定与这些所有线段相交。所以我们就可以将问题转化为求是否存在一条直线与这些所有线段相交。n<=100,所以我们可以去枚举所有线段的两个端点假设为所求直线,然后用叉积去判断该直线是否与所有线段相交。
注意:可能会枚举到重合的点,特殊考虑下。

代码如下

#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=105;
const double eps=1e-8;

int n;

int dcmp(double x) {
    if(fabs(x)<eps) return 0;
    return x<0?-1: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.x + y*b.y;
    }
    double operator ^(const Point &b)const {
        return x*b.y - y*b.x;
    }
};
struct Line {
    Point a,b;
    Line() {}
    Line(Point _a,Point _b) {
        a=_a;
        b=_b;
    }
};

Line line[N];
double dist(Point a,Point b)
{
    return sqrt((b-a)*(b-a));//返回两个点之间的距离   
} 
double xmult(Point p0,Point p1,Point p2) {
    return (p1-p0)^(p2-p0);
}
bool check(Line L1,Line L2)//判断直线L1和线段L2是否相交 
{
    return dcmp(xmult(L2.a,L1.a,L1.b))*dcmp(xmult(L2.b,L1.a,L1.b))<=0;
}
bool solve(Line L1)
{
    if(dcmp(dist(L1.a,L1.b))==0) return false;//如果所选的两个点重合,即不能构成一条直线 
    for(int i=0;i<n;i++)
        if(check(L1,line[i])==false)
        return false;
    return true; 
}

int main() 
{
    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));
        }
        int f=0;
        for(int i=0;i<n;i++)
        if(!f)
        for(int j=0;j<n;j++)
        {
            if(solve(Line(line[i].a,line[j].a))||solve(Line(line[i].a,line[j].b))||
            solve(Line(line[i].b,line[j].a))||solve(Line(line[i].b,line[j].b)))
            {
                f=1;
                break;
            }

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

余额充值