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 (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!
题意:
给我们n条线段,问是否存在一条直线,将这些线段投影到直线上有至少有一个公共的交点。
思路:
我们可以转化思考,构造一条直线,如果这条直线交于全部的线段,那么这个投影点就是这些交点,而这条直线必定经过这些线段得两个端点。枚举任意两个端点,判断是否存在与全部线段相交即可。
1.判断直线(ab)与线段(cd)相交
根据上面的两种情况,我们直接可以用向量的叉积来判断是否相交了。
2.为什么这条直线必定经过这些线段上的两个端点呢?
我们假设将这些线段旋转平移成这个样子,要使得这个投影点O一直存在,那么随便平移,不能超过两个端点。
AC代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define eps 1e-8
#define inf 99999999
const int maxn = 105;
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
}p[maxn];
typedef Point Vector;
int dcmp(double x){
if(fabs(x)<eps)
return 0;
else
return x<0?-1:1;
}
Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Point A, Point B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A, double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A, double p) {return Vector(A.x/p,A.y/p);}
bool operator == (const Point& a, const Point& b){return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;}
double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
struct Line{
Point v,p;
Line(){}
Line(Point v,Point p):v(v),p(p){}
Point point(double t) {
return v+(p-v)*t;
}
};
typedef Line Segment;
Segment line[maxn];
bool LineIntersection(Point a, Point b, Point c, Point d){//直线ab与线段cd相交
return Cross(b - a, c - a) * Cross(b - a, d - a) <= 0;
}
bool cheak(Point a, Point b, int n){
for (int i = 0; i < n; ++i){
if(! LineIntersection(a, b, line[i].v, line[i].p)) return false;
}
return true;
}
void solve(){
int n, cnt = 0;
double x1, y1, x2, y2;
Point a,b;
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
p[cnt++] = Point(x1, y1);
p[cnt++] = Point(x2, y2);
line[i] = Line(Point(x1, y1), Point(x2, y2));
}
bool flag = false;
for (int i = 0; i < cnt; ++i){
flag = false;
for (int j = i + 1; j < cnt; ++j){
if (p[i] == p[j]) continue;
if (cheak(p[i], p[j], n)) flag = true;
if (flag) break;
}
if (flag) break;
}
if (flag) puts("Yes!");
else puts("No!");
}
int main(){
int t;
scanf("%d", &t);
for (int i = 0; i < t; ++i) solve();
return 0;
}