POJ1912_A highway and the seven dwarfs_判断凸包与直线是否相交
来源:互联网 发布:超图软件工资怎么样 编辑:程序博客网 时间:2024/05/22 12:30
题意
给出 n 个点和若干条直线,每一条直线用两个点的形式给出。对每一条直线,问所有点是不是在这条直线的同一侧。题目保证直线不会经过n个点。
思路
首先就有一个特殊情况。因为 n >= 0。当 n <= 1 的时候,可以直接判定为对每一条直线,n个点都在它的同一侧。
对于n > 1的情况,显然是求凸包与直线是否相交的问题。这里利用了凸包的一个性质。
对于凸包上相邻的两个点构成的向量的极角,取 [-pi/2, 3/2pi]时,是单调递增的。
求出凸包上边的极角后,
用二分法,找出极角分别大于给出直线的两个方向向量的第一个向量, 这两个向量的起点,就是在垂直于这条直线的方向上距离最远的两个点。
只需要判断这两个点是否在直线的同一侧,就能判断这条直线是不是穿过凸包。
可以画画图理解一下。
链接
http://poj.org/problem?id=1912
代码
#include<cstdio>#include<iostream>#include<vector>#include<algorithm>#include<cmath>using namespace std;const int maxn = 1e5 + 10;const double pi = acos(-1.0);struct P{ double x, y; P(){} P(double x, double y): x(x), y(y){} bool operator < (const P &a) const{ if(x != a.x) return x < a.x; return y < a.y; } P operator - (const P &a) const{ return P(x - a.x, y - a.y); } double det(const P &a) const{ return x * a.y - y * a.x; } double angle(){ double angle = atan2(y, x); if(angle < -pi / 2) angle += 2 * pi; return angle; }};int n;P ps[maxn], qs[maxn];double angle[maxn];P p1, p2;int tb;int convex_hull(){ sort(ps, ps + n); int k = 0; for(int i = 0; i < n; ++i){ while(k > 1 && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) <= 0) --k; qs[k++] = ps[i]; } for(int i = n - 2, t = k; i >= 0; --i){ while(k > t && (qs[k-1]- qs[k-2]).det(ps[i] - qs[k-1]) <= 0) --k; qs[k++] = ps[i]; } return k-1;}inline int getp(double a){ return lower_bound(angle, angle + tb, a) - angle;}bool inter(){ if(n <= 1) return false; int a = getp((p1 - p2).angle()); int b = getp((p2 - p1).angle()); if((p1 - p2).det(qs[a] - p2) * (p1 - p2).det(qs[b] - p2) <= 0) return true; return false;}int main(){// freopen("in.txt", "r", stdin); scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%lf %lf", &ps[i].x, &ps[i].y); } tb = convex_hull(); for(int i = 0; i < tb; ++i){ angle[i] = (qs[i+1] - qs[i]).angle(); } while(scanf("%lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y) == 4){ if(inter()) puts("BAD"); else puts("GOOD"); } return 0;}
阅读全文
0 0
- POJ1912_A highway and the seven dwarfs_判断凸包与直线是否相交
- POJ 1912 A highway and the seven dwarfs (凸包&O(logN)判断直线是否与凸包相交)
- POJ 1912 A highway and the seven dwarfs(O(log N)求直线与凸包是否相交)
- poj1912:A highway and the seven dwarfs(凸包)
- poj1912 A highway and the seven dwarfs【凸包+二分】
- 判断两直线是否相交
- 判断两直线是否相交
- POJ 1912 A highway and the seven dwarfs 已翻译
- POJ3304_Segments_叉积::判断直线与线段是否相交
- 判断直线与矩形相交
- POJ 1269 判断直线与直线相交
- 判断直线与线段是否相交,相交则输出交点x轴坐标
- 51nod 判断线段是否相交 poj Segments直线与多条线段相交
- poj 1410(判断直线是否相交)
- 判断平面上两条直线是否相交
- 判断两条直线是否相交
- 判断两条直线是否相交
- CodeForces - 593B Anton and Lines (数学方程&技巧) 判断直线是否相交
- Unity3D更改游戏鼠标样式
- 数据库建表方法
- 【Python编程:从入门到实践】第十章:文件和异常
- PAT (Basic Level) Practise (中文)1055. 集体照 (25)
- 一个看似简单的数字交换问题
- POJ1912_A highway and the seven dwarfs_判断凸包与直线是否相交
- scikit-learn中gridSearchCV 的使用
- 博弈论(1):囚徒困境中的博弈论
- java常用排序算法
- spring in action笔记(四)——高级装配
- if there is a error "SSSE3 instruction set not enabled"
- Python利用Scrapy爬取智联招聘和前程无忧的招聘数据
- GDB分析ELF文件常用的调试技巧
- android 编写发表帖子的页面