POJ 3304 计算几何

本文提供了一个解决几何问题的算法,旨在判断给定的二维空间内的多个线段是否至少存在一条直线使得所有线段投影后至少有一个公共点。通过输入测试案例的数量、线段数量及线段的端点坐标,输出结果为存在这样的直线则显示Yes!,否则显示No!。该算法采用计算几何方法,包括点与线段之间的相对位置判断和直线的构建。

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

Segments
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 9564  Accepted: 2943

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

Amirkabir University of Technology Local Contest 2006

 

<span style="color:#6600cc;">/**************************************
     author    : Grant Yuan
     time      : 2014/8/19 16:53
     algorithm : 计算几何
     source    : POJ 3304
***************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define MAX 107
#define eps 1e-8
#include<cmath>

using namespace std;

struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point&b)
    {
        return Point(x-b.x,y-b.y);
    }
    double operator *(const Point&b)
    {
       return x*b.x+y*b.y;
    }
    double operator ^(const Point&b)
    {
        return x*b.y-y*b.x;
    }
    bool operator ==(const Point&b)
    {
        return(x==b.x&&y==b.y);
    }
};

struct Line
{
    Point p1,p2;
    Line(){}
    Line(Point a,Point b){p1=a;p2=b;}
};

Point P[2*MAX];
Line L[MAX];

bool check(Point q0,Point q1,Point q2,Point q3)
{
    if(fabs(q0.x-q1.x)<eps&&fabs(q0.y-q1.y)<eps)
        return false;
    int t1=0,t2=0;
    if(((q1-q0)^(q2-q0))<-eps) t1=-1;
    else if(((q1-q0)^(q2-q0))>eps) t1=1;

     if(((q1-q0)^(q3-q0))<-eps) t2=-1;
    else if(((q1-q0)^(q3-q0))>eps) t2=1;
    if(t1*t2==-1||t1==0||t2==0)
        return true;
    return false;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
      int n;
      scanf("%d",&n);
      for(int i=0;i<2*n;i++)
      {
          scanf("%lf%lf",&P[i].x,&P[i].y);
      }
      if(n==1||n==2) printf("Yes!\n");
      else{
      for(int i=0;i<2*n;i++)
      {
          if(i%2==0) L[i/2].p1=P[i];
          else L[i/2].p2=P[i];
      }
      Point q0,q1;
      bool ans=0;
      for(int i=0;i<2*n-1;i++)
      {
        q0=P[i];
        for(int j=i+1;j<2*n;j++)
        {
        if(P[j].x==q0.x&&P[j].y==q0.y) continue;
        q1=P[j];
        bool flag=1;
        for(int k=0;k<n;k++)
        {
            if(!check(q0,q1,L[k].p1,L[k].p2)){flag=0; break;}
        }
        if(flag) {ans=1;break;}
        }
        if(ans) break;
      }
       if(ans) printf("Yes!\n");
      else printf("No!\n");
    }}
    return 0;
}
</span>


 

转载于:https://www.cnblogs.com/codeyuan/p/4254453.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值