POJ3304(KB13-C 计算几何)

时间:2024-06-23 10:37:38

Segments

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15335   Accepted: 4862

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

Source

询问是否存在一条直线与所有线段相交
暴力线段的断点构成直线,判断是否可行。
 //2017-08-30
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int N = ;
const double EPS = 1e-; struct Point{
double x, y;
Point(){}
Point(double _x, double _y):x(_x), y(_y){}
Point(const Point &p):x(p.x), y(p.y){}
//a-b为向量ba
Point operator- (const Point &b) const {
return Point(x-b.x, y-b.y);
}
//向量叉积
double operator^ (const Point &b) const {
return x*b.y - y*b.x;
}
//向量点积
double operator* (const Point &b) const {
return x*b.x + y*b.y;
}
}; struct Line{
Point a, b;
Line(){}
Line(Point _a, Point _b):a(_a), b(_b){}
void set_a_b(const Point &_a, const Point &_b){
a = _a;
b = _b;
}
}seg[N]; //三态函数
int sgn(double x){
if(fabs(x) < EPS)return ;
if(x < )return -;
else return ;
} //input:Point a;Point b
//output:distance a、b两点间的距离
//note:该函数取名distance会编译错误
double dist(Point a, Point b){
return sqrt((a-b)*(a-b));
} //input:l1 直线l1;l2 线段l2
//output:true 直线l1与线段l2相交;false 直线l1与线段l2不想交
bool seg_inter_line(Line l1, Line l2){
return sgn((l2.a-l1.b)^(l1.a-l1.b))*sgn((l2.b-l1.b)^(l1.a-l1.b)) <= ;
} int n;
//input:直线line
//output:true 直线与所有线段都相交
bool all_inter(Line line){
for(int i = ; i < n; i++)
if(!seg_inter_line(line, seg[i]))
return false;
return true;
} bool check(){
Line line;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i == j)continue;
if(dist(seg[i].a, seg[j].a) > EPS){
line.set_a_b(seg[i].a, seg[j].a);
if(all_inter(line))return true;
}
if(dist(seg[i].a, seg[j].b) > EPS){
line.set_a_b(seg[i].a, seg[j].b);
if(all_inter(line))return true;
}
if(dist(seg[i].b, seg[j].a) > EPS){
line.set_a_b(seg[i].b, seg[j].a);
if(all_inter(line))return true;
}
if(dist(seg[i].b, seg[j].b) > EPS){
line.set_a_b(seg[i].b, seg[j].b);
if(all_inter(line))return true;
}
}
}
return false;
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputC.txt", "r", stdin);
int T;
cin>>T;
while(T--){
cin>>n;
for(int i = ; i < n; i++){
cin>>seg[i].a.x>>seg[i].a.y>>seg[i].b.x>>seg[i].b.y;
}
if(check() || n == || n == )cout<<"Yes!"<<endl;
else cout<<"No!"<<endl;
} return ;
}