Segments

寻找共线段投影

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


#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
//#include<bits/stdc++.h>
#define eps 1e-8
using namespace std;
struct Line
{
   double x1,y1;
   double x2,y2;
}line[110];
double dis(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double cross(double x1,double y1,double x2,double y2,double x,double y)
{
    return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
}
int t,n;
bool  jude(double x1,double y1,double x2,double y2)
{
    if(dis(x1,y1,x2,y2)<eps) return 0;
    for(int i=0;i<n;i++)
    {
        if(cross(x1,y1,x2,y2,line[i].x1,line[i].y1)*
           cross(x1,y1,x2,y2,line[i].x2,line[i].y2)>eps)
            return 0;
    }
    return 1;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);
        }
        if(n<3)
        {
            printf("Yes!\n");
            continue;
        }
        int flag=0;
      for(int i=0;i<n&&!flag;i++)
      {
          for(int j=i+1;j<n&&!flag;j++)
          {
              if(jude(line[i].x1,line[i].y1,line[j].x1,line[j].y1)||
                 jude(line[i].x1,line[i].y1,line[j].x2,line[j].y2)||
                 jude(line[i].x2,line[i].y2,line[j].x1,line[j].y1)||
                 jude(line[i].x2,line[i].y2,line[j].x2,line[j].y2))
                    flag=1;
          }
      }
      if(flag)
        printf("Yes!\n");
      else
        printf("No!\n");


    }
}


由于没有提供具体的参考引用内容,下面是基于常见知识对All Pairs Segments的介绍。 All Pairs Segments通常可能指的是在某种数据处理或者几何、图论等领域中,对所有的成对线段进行处理或者分析的一种概念。 在几何场景中,假设有一组线段,All Pairs Segments就是要考虑这组线段中任意两条线段之间的关系,比如判断它们是否相交、计算它们之间的距离等。以下是一个简单的Python代码示例,用于判断两条线段是否相交: ```python def orientation(p, q, r): val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]) if val == 0: return 0 # 共线 elif val > 0: return 1 # 顺时针 else: return 2 # 逆时针 def do_intersect(p1, q1, p2, q2): o1 = orientation(p1, q1, p2) o2 = orientation(p1, q1, q2) o3 = orientation(p2, q2, p1) o4 = orientation(p2, q2, q1) if o1 != o2 and o3 != o4: return True # 特殊情况 if o1 == 0 and on_segment(p1, p2, q1): return True if o2 == 0 and on_segment(p1, q2, q1): return True if o3 == 0 and on_segment(p2, p1, q2): return True if o4 == 0 and on_segment(p2, q1, q2): return True return False def on_segment(p, q, r): return (q[0] <= max(p[0], r[0]) and q[0] >= min(p[0], r[0]) and q[1] <= max(p[1], r[1]) and q[1] >= min(p[1], r[1])) # 示例线段 segment1 = ((0, 0), (1, 1)) segment2 = ((0, 1), (1, 0)) print(do_intersect(segment1[0], segment1[1], segment2[0], segment2[1])) ``` 在图论或者数据分析中,All Pairs Segments可能表示对所有成对的数据段进行某种计算或者度量,比如计算每对数据段之间的相似度等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值