博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3304(KB13-C 计算几何)
阅读量:5154 次
发布时间:2019-06-13

本文共 4097 字,大约阅读时间需要 13 分钟。

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

321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0

Sample Output

Yes!Yes!No!

Source

 
询问是否存在一条直线与所有线段相交
暴力线段的断点构成直线,判断是否可行。
1 //2017-08-30  2 #include 
3 #include
4 #include
5 #include
6 #include
7 8 using namespace std; 9 10 const int N = 110; 11 const double EPS = 1e-8; 12 13 struct Point{ 14 double x, y; 15 Point(){} 16 Point(double _x, double _y):x(_x), y(_y){} 17 Point(const Point &p):x(p.x), y(p.y){} 18 //a-b为向量ba 19 Point operator- (const Point &b) const { 20 return Point(x-b.x, y-b.y); 21 } 22 //向量叉积 23 double operator^ (const Point &b) const { 24 return x*b.y - y*b.x; 25 } 26 //向量点积 27 double operator* (const Point &b) const { 28 return x*b.x + y*b.y; 29 } 30 }; 31 32 struct Line{ 33 Point a, b; 34 Line(){} 35 Line(Point _a, Point _b):a(_a), b(_b){} 36 void set_a_b(const Point &_a, const Point &_b){ 37 a = _a; 38 b = _b; 39 } 40 }seg[N]; 41 42 //三态函数 43 int sgn(double x){ 44 if(fabs(x) < EPS)return 0; 45 if(x < 0)return -1; 46 else return 1; 47 } 48 49 //input:Point a;Point b 50 //output:distance a、b两点间的距离 51 //note:该函数取名distance会编译错误 52 double dist(Point a, Point b){ 53 return sqrt((a-b)*(a-b)); 54 } 55 56 //input:l1 直线l1;l2 线段l2 57 //output:true 直线l1与线段l2相交;false 直线l1与线段l2不想交 58 bool seg_inter_line(Line l1, Line l2){ 59 return sgn((l2.a-l1.b)^(l1.a-l1.b))*sgn((l2.b-l1.b)^(l1.a-l1.b)) <= 0; 60 } 61 62 int n; 63 //input:直线line 64 //output:true 直线与所有线段都相交 65 bool all_inter(Line line){ 66 for(int i = 0; i < n; i++) 67 if(!seg_inter_line(line, seg[i])) 68 return false; 69 return true; 70 } 71 72 bool check(){ 73 Line line; 74 for(int i = 0; i < n; i++){ 75 for(int j = 0; j < n; j++){ 76 if(i == j)continue; 77 if(dist(seg[i].a, seg[j].a) > EPS){ 78 line.set_a_b(seg[i].a, seg[j].a); 79 if(all_inter(line))return true; 80 } 81 if(dist(seg[i].a, seg[j].b) > EPS){ 82 line.set_a_b(seg[i].a, seg[j].b); 83 if(all_inter(line))return true; 84 } 85 if(dist(seg[i].b, seg[j].a) > EPS){ 86 line.set_a_b(seg[i].b, seg[j].a); 87 if(all_inter(line))return true; 88 } 89 if(dist(seg[i].b, seg[j].b) > EPS){ 90 line.set_a_b(seg[i].b, seg[j].b); 91 if(all_inter(line))return true; 92 } 93 } 94 } 95 return false; 96 } 97 98 int main() 99 {100 std::ios::sync_with_stdio(false);101 //freopen("inputC.txt", "r", stdin);102 int T;103 cin>>T;104 while(T--){105 cin>>n;106 for(int i = 0; i < n; i++){107 cin>>seg[i].a.x>>seg[i].a.y>>seg[i].b.x>>seg[i].b.y;108 }109 if(check() || n == 1 || n == 2)cout<<"Yes!"<

 

转载于:https://www.cnblogs.com/Penn000/p/7453891.html

你可能感兴趣的文章
开源自己写的一个拖拽库,兼容到IE8+
查看>>
mvc中的表现和数据分离怎么理解?
查看>>
启动elasticsearch报错
查看>>
Socket详解
查看>>
Java-004-变量类型和修饰符详解
查看>>
JAVA字符串格式化-String.format()的使用
查看>>
[原创]windows 部署SS server 出现的错误.
查看>>
mysql 索引
查看>>
第13组_16通信3班_045_OSPFv3作业
查看>>
调试相关连接资源
查看>>
lftp用法手册
查看>>
android图像处理系列之四-- 给图片添加边框(上)
查看>>
Android利用ViewFlipper实现屏幕切换动画效果
查看>>
css 中在图片中加入文字的方法
查看>>
简单电商购物程序
查看>>
setHeader方法的参数说明
查看>>
感知机:Perceptron Learning Algorithm
查看>>
返回vector指针案例
查看>>
《About Multi-Touch(多点触摸是个什么东西?)》:Community Core Vision(CCV) 1.3 全指南...
查看>>
31 数组划分
查看>>