题意:给n个线段,问是否存在这样的一条直线,满足这n个线段在投影有公共点
思路:如果存在这样的直线,那么这样的直线一定是一组平行的直线都可以(投影),投影有公共点,从公共点做直线的垂线,那么这条垂线一定和所有的线段相交,也就是如果存在一个直线和所有的线段相交就可以。如果存在这样的垂线,考虑如果稍微偏移一定的角度,一定偏移到了某条线段的端点的时候不能再偏移,所以只要枚举端点就可以了(开始时枚举的是某起点-某终点,后来才发现应该是某端点到某端点)
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
const double INF = 1e200;
const double EP = 1e-8;
const int maxn = 5100;
const double PI = acos(-1);
struct POINT{
double x;
double y;
POINT(double a = 0,double b = 0){x = a;y = b;}
};///点 定义
struct SEGMENT{///line segment
POINT s;
POINT e;
SEGMENT(POINT a,POINT b){s = a;e = b;}
SEGMENT(){}
};///线段 定义
struct LINE{
double a;
double b;
double c;
LINE(double da,double db,double dc){a = da;b = db;c = dc;}///一般式
LINE(double x1,double y1,double x2,double y2){///根据两个点求出一般式
a = y1 - y2;b = x2 - x1;c = x1*y2 - x2*y1;
if(a < 0){a*=-1;b*=-1;c*=-1;}
}
};///ax + by + c = 0&&a >= 0
int multiply(POINT sp,POINT ep,POINT op){
return ((sp.x - op.x)*(ep.y - op.y) - (ep.x - op.x)*(sp.y - op.y));
}///向量op->sp X op->ep的叉乘,小于0:ep在op->sp顺时针方向//大于0:0:ep在op->sp逆时针方向//等于0:三点共线
int lineintersect(LINE l1,LINE l2,POINT &p){
double d = l1.a*l2.b-l2.a*l1.b;
double d2 = l1.a*l2.c-l2.a*l1.c;
double d3 = l1.b*l2.c-l2.b*l1.c;
if(fabs(d) < EP&&fabs(d2) < EP&&fabs(d3) < EP)return 2;
p.x = (l2.c*l1.b-l1.c*l2.b)/d;
p.y = (l2.a*l1.c-l1.a*l2.c)/d;
if(fabs(d) < EP)return 0;
return 1;
}///求两直线交点,有交点返回1和交点,没有返回0,重合返回2
double point_dis(POINT a,POINT b){
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
int n;
SEGMENT seg[110];
bool judge(POINT a,POINT b){
if(fabs(a.x - b.x) < EP&&fabs(a.y - b.y) < EP)return false;
LINE now(a.x,a.y,b.x,b.y);
for(int k = 0;k < n;k ++){
LINE tmp(seg[k].s.x,seg[k].s.y,seg[k].e.x,seg[k].e.y);
POINT pp;
int r = lineintersect(tmp,now,pp);
if(r == 2)continue;
else if(r == 0)return false;
else if(fabs(point_dis(seg[k].s,pp) + point_dis(seg[k].e,pp) - point_dis(seg[k].s,seg[k].e)) > EP)return false;
}
return true;
}
int main(){
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i = 0;i < n;i ++){
POINT a,b;
scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
//cin>>a.x>>a.y>>b.x>>b.y;
seg[i].s = a;
seg[i].e = b;
}
if(n == 1||n == 2){
puts("Yes!");continue;
}
bool ok = 0;
for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++){
ok = judge(seg[i].s,seg[j].s);if(ok)break;
ok = judge(seg[i].s,seg[j].e);if(ok)break;
ok = judge(seg[i].e,seg[j].s);if(ok)break;
ok = judge(seg[i].e,seg[j].e);if(ok)break;
}if(ok)break;
}
if(ok)puts("Yes!");
else puts("No!");
}
return 0;
}