hdu 3304(直线与线段相交)

本文介绍了一种算法,用于判断二维空间中是否存在一条直线,使得所有给定线段投影到该直线上后,各投影线段至少有一个公共点。通过枚举线段端点并检查是否满足条件来实现。
Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12042 Accepted: 3808

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 x1y1x2y2 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!
可以理解成如果直线合法,然后旋转的话总是会和某两条直线的端点相交,枚举端点。找到一条合法的。
这题用规范相交就过了。
///判断直线与线段相交
///做法:枚举每两个端点,要是存在一条直线经过这两个端点并且和所有线段相交就OK,但是不能为重合点.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps = 1e-8;
const int N = 105;
struct Point
{
    double x,y;
};
struct Line{
    Point a,b;
}line[N];
int n;

double cross(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double dis(Point a,Point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool isCross(Point a,Point b,Point c,Point d){///这个函数判断直线和线段是否相交,直线是无线延长的,所以一个点在这一边一个点在那一边就OK
    if(cross(a,b,c)*cross(a,b,d)>0) return true;
    return false;
}
bool check(Point a,Point b){
    if(sqrt(dis(a,b))<eps) return false;//这里记得讨论
    for(int i=0;i<n;i++){
        if(isCross(a,b,line[i].a,line[i].b)){
            return false;
        }
    }
    return true;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
        }
         if(n==1||n==2) {
            printf("Yes!\n");
            continue;
        }
        bool flag=false;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(check(line[i].a,line[j].a)||check(line[i].b,line[j].b)||check(line[i].a,line[j].b)||check(line[i].b,line[j].a)){
                    flag = true;
                    break;
                }
            }
            if(flag) break;
        }
        if(flag) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5448114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值