poj3304-Segments-判断直线和线段是否相交

本文深入探讨了一种特定算法在处理复杂数据集时的效能与优化策略,结合实际案例分析了该算法在不同场景下的表现,并提出了针对性的改进措施。通过对比分析,揭示了算法在提高数据处理效率、提升准确性方面的潜力与挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Segments

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!

题意:是否存在一条直线,使得所有线段在其上的投影交于同一点
相当于求是否存在一条直线l交于所有线段,作直线l的垂线m,则m即为所求的直线
将所有线段的两个端点列出来,连接任意两个端点,形成一条直线,然后判断此直线和所有线段是否相交

#include<stdio.h>
#include<math.h>
const double eps=1e-8;
struct point{
    double x,y;
}Seg[1000];
int equ(point a,point b)
{
    double dis;
    dis=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    if(sqrt(dis) < eps) return 0;
    else return 1;
}
double fun(point a,point b,point c)
{
    return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
int judge(int n)
{
    for(int i=0; i < n ; i++)
    {
        for(int j=i+1 ; j < n ; j++)
        {
            if(equ(Seg[i],Seg[j])==0) continue;
            for(int k=0; k < n ; k+=2)
            {
                double d1,d2;
                d1=fun(Seg[i],Seg[j],Seg[k]);
                d2=fun(Seg[i],Seg[j],Seg[k+1]);
                if(d1*d2>0)
                    break;
                if(k==n-2)
                    return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int T,n;
    double x1,y1,x2,y2;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int j=0;
        for(int i=0 ; i < n ; i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            Seg[j].x=x1; Seg[j].y=y1;
            Seg[j+1].x=x2; Seg[j+1].y=y2;
            j+=2;
        }
        if(judge(2*n)) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值