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
6
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0
-1 -1 -8 9
-1 -1 -7 -6
-1 -1 4 -2
No!
附上AC代码:
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
#define Root 1, m, 1
#define Node l, r, node
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 20071027;
const int maxn = 1e3 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
const double esp = 1e-8;
struct Point { double x, y; };
typedef Point Vector;
Vector operator + (Vector a, Vector b) { return { a.x + b.x, a.y + b.y }; }
Vector operator - (Vector a, Vector b) { return { a.x - b.x, a.y - b.y }; }
Vector operator * (Vector a, double p) { return { a.x * p, a.y * p}; }
Vector operator / (Vector a, double p) { return { a.x / p, a.y / p}; }
bool operator < (Vector a, Vector b) { return a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y; }
bool operator == (Vector a, Vector b) { return fabs(a.x - b.x) < esp && fabs(a.y - b.y) < esp; }
// 点积,模长,角度,叉积,逆时针旋转
double dotProduct (Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double getLen (Vector a) { return sqrt (a.x * a.x + a.y * a.y); }
double angle ( Vector a, Vector b) { return acos( dotProduct(a, b) / getLen(a) / getLen(b)); }
double CrossProduct (Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
Vector Rotate (Vector a, double rad) { return { a.x*cos(rad) - a.y*sin(rad), a.x*sin(rad) + a.y*cos(rad) }; }
// 判断线段AB是否跨立直线MN
bool straddle (Point A, Point B, Point M, Point N) {
Vector p0 = { N.x - M.x , N.y - M.y }; // 向量 MN
Vector p1 = { A.x - M.x , A.y - M.y }; // 向量 MA
Vector p2 = { B.x - M.x , B.y - M.y }; // 向量 MB
return CrossProduct(p1, p0) * CrossProduct(p2, p0) <= 0;
}
// 判断 线段AB为对角线构成的矩形 和 MN线段为对角线构成的矩形 是否存在重叠
bool overlap (Point A, Point B, Point M, Point N){
Point P = { max(min(A.x, B.x), min(M.x, N.x)), max(min(A.y, B.y), min(M.y, N.y))};
Point Q = { min(max(A.x, B.x), max(M.x, N.x)), min(max(A.y, B.y), max(M.y, N.y))};
return P.x <= Q.x && P.y <= Q.y;
}
// 判断线段AB和线段MN是否相交
bool intersect (Point A, Point B, Point M, Point N) {
return straddle(A, B, M, N) && straddle(M, N, A, B) && overlap(A, B, M, N);
}
// 上面为向量基本模板
Point a[2][maxn];
int n;
bool check(Point X, Point Y){
if(sqrt((X.x-Y.x)*(X.x-Y.x) + (X.y-Y.y)*(X.y-Y.y)) <= esp) return false; // 很关键,wa了八发才找到原因,自己理解
for(int i=0; i<n; i++) if(!straddle(a[0][i], a[1][i], X, Y)) return false; // 由跨立判断线段ai是否与直线XY相交
return true;
}
int main () {
bool p = intersect({0, 0}, {0, 0}, {1, 2}, {4, 8});
int T; scanf("%d", &T);
while ( T -- ){
scanf("%d", &n);
Point all[maxn]; int cnt = 0;
for(int i=0; i<n; i++) {
scanf("%lf%lf%lf%lf", &a[0][i].x, &a[0][i].y, &a[1][i].x, &a[1][i].y);
all [cnt ++] = a[0][i]; all [cnt ++] = a[1][i];
}
bool flag = false;
for(int i=0; i<2*n; i++) {
if(flag) break;
for(int j=i+1; j<2*n; j++) if(check(all[i], all[j])) {
flag = true; break;
}
}
printf(flag ? "Yes!\n" : "No!\n");
}
return 0;
}