Codeforces 166B - Polygon (判断凸包位置关系)
Codeforces Round #113 (Div. 2)
题目链接:Polygons
You've got another geometrical task. You are given two non-degenerate polygons \(A\) and \(B\) as vertex coordinates. Polygon \(A\) is strictly convex. Polygon \(B\) is an arbitrary polygon without any self-intersections and self-touches. The vertices of both polygons are given in the clockwise order. For each polygon no three consecutively following vertices are located on the same straight line.
Your task is to check whether polygon \(B\) is positioned strictly inside polygon \(A\). It means that any point of polygon \(B\) should be strictly inside polygon \(A\). "Strictly" means that the vertex of polygon \(B\) cannot lie on the side of the polygon \(A\).
Input
The first line contains the only integer \(n (3 \le n \le 10^5)\) — the number of vertices of polygon \(A\). Then \(n\) lines contain pairs of integers \(x_i, y_i (|x_i|, |y_i| \le 10^9)\) — coordinates of the i-th vertex of polygon \(A\). The vertices are given in the clockwise order.
The next line contains a single integer \(m (3 \le m \le 2·10^4)\) — the number of vertices of polygon \(B\). Then following \(m\) lines contain pairs of integers \(x_j, y_j (|x_j|, |y_j| \le 10^9)\) — the coordinates of the \(j\)-th vertex of polygon \(B\). The vertices are given in the clockwise order.
The coordinates of the polygon's vertices are separated by a single space. It is guaranteed that polygons \(A\) and \(B\) are non-degenerate, that polygon \(A\) is strictly convex, that polygon \(B\) has no self-intersections and self-touches and also for each polygon no three consecutively following vertices are located on the same straight line.
Output
Print on the only line the answer to the problem — if polygon \(B\) is strictly inside polygon \(A\), print "YES", otherwise print "NO" (without the quotes).
Examples
input
6
-2 1
0 3
3 3
4 1
3 -2
2 -2
4
0 1
2 2
3 1
1 0
output
YES
input
5
1 2
4 2
3 -3
-2 -2
-2 1
4
0 1
1 2
4 1
2 -1
output
NO
input
5
-1 2
2 3
4 1
3 -2
0 -3
5
1 0
1 1
3 1
5 -1
2 -1
output
NO
Solution
题意
给定两个凸包 \(A\) 和 \(B\)。判断凸包 \(B\) 是否严格在凸包 \(A\) 内。
题解
对凸包 \(A\) 和 \(B\) 的所有点构造凸包,判断该凸包是否等于凸包 \(A\)。若相等,则凸包 \(B\) 严格在凸包 \(A\) 内。
Code
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dcmp(double x) {
if (fabs(x) <= eps)
return 0;
return x > 0 ? 1 : -1;
}
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point a) {
return Point(a.x + x, a.y + y);
}
Point operator-(Point a) {
return Point(x - a.x, y - a.y);
}
bool operator<(const Point &a) const {
if (x == a.x)
return y < a.y;
return x < a.x;
}
bool operator==(const Point &a) const {
if (fabs(x - a.x) < eps && fabs(y - a.y) < eps)
return 1;
return 0;
}
bool operator!=(const Point &a) const {
if ((*this) == a)
return 0;
return 1;
}
double length() {
return sqrt(x * x + y * y);
}
};
typedef Point Vector;
double cross(Vector a, Vector b) {
return a.x * b.y - a.y * b.x;
}
double dot(Vector a, Vector b) {
return a.x * b.x + a.y * b.y;
}
bool isclock(Point p0, Point p1, Point p2) {
Vector a = p1 - p0;
Vector b = p2 - p0;
if (dcmp(cross(a, b)) <= 0) // 为了让凸包边上可以有点
return true;
return false;
}
double getDistance(Point a, Point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
typedef vector<Point> Polygon;
Polygon Andrew(Polygon s) {
Polygon u, l;
if(s.size() < 3) return s;
sort(s.begin(), s.end());
u.push_back(s[0]);
u.push_back(s[1]);
l.push_back(s[s.size() - 1]);
l.push_back(s[s.size() - 2]);
for(int i = 2 ; i < s.size() ; ++i) {
for(int n = u.size() ; n >= 2 && !isclock(u[n - 2], u[n - 1], s[i]); --n) {
u.pop_back();
}
u.push_back(s[i]);
}
for(int i = s.size() - 3 ; i >= 0 ; --i) {
for(int n = l.size() ; n >=2 && !isclock(l[n-2],l[n-1],s[i]); --n) {
l.pop_back();
}
l.push_back(s[i]);
}
for(int i = 1 ; i < u.size() - 1 ; i++) l.push_back(u[i]);
return l;
}
// 判断点在线段上
bool OnSegment(Point p, Point a1, Point a2) {
return dcmp(cross(a1 - p, a2 - p)) == 0 && dcmp(dot(a1 - p, a2 - p)) < 0;
}
// 判断点在凸包内
int isPointInPolygon(Point p, vector<Point> s) {
int wn = 0, cc = s.size();
for (int i = 0; i < cc; i++) {
Point p1 = s[i];
Point p2 = s[(i + 1) % cc];
if (p1 == p || p2 == p || OnSegment(p, p1, p2)) return -1;
int k = dcmp(cross(p2 - p1, p - p1));
int d1 = dcmp(p1.y - p.y);
int d2 = dcmp(p2.y - p.y);
if (k > 0 && d1 <= 0 && d2 > 0) wn++;
if (k < 0 && d2 <= 0 && d1 > 0) wn--;
}
if (wn != 0) return 1;
return 0;
}
int main() {
Polygon A, B;
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
double x, y;
scanf("%lf%lf", &x, &y);
A.push_back(Point(x, y));
B.push_back(Point(x, y));
}
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
double x, y;
scanf("%lf%lf", &x, &y);
B.push_back(Point(x, y));
}
A = Andrew(A);
B = Andrew(B);
if(A.size() != B.size()) {
printf("NO\n");
return 0;
}
int flag = 1;
sort(A.begin(), A.end());
sort(B.begin(), B.end());
for(int i = 0; i < A.size(); ++i) {
if(A[i] != B[i]) {
flag = 0;
break;
}
}
if(flag) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
Codeforces 166B - Polygon (判断凸包位置关系)的更多相关文章
- JS魔法堂:判断节点位置关系
一.前言 在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关系,通过jQuery我们可以轻松搞定,但原生JS呢?下面我将整理各种判断方法,以供日后查阅. 二 ...
- POJ 1269 Intersecting Lines (判断直线位置关系)
题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a li ...
- Cupid's Arrow---hdu1756(判断点与多边形的位置关系 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1756 题意:中文题,套模板即可: /* 射线法:判断一个点是在多边形内部,边上还是在外部,时间复杂度为 ...
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
- Intersecting Lines (计算几何基础+判断两直线的位置关系)
题目链接:http://poj.org/problem?id=1269 题面: Description We all know that a pair of distinct points on a ...
- poj 1269 判断直线的位置关系
题目链接 题意 判断两条直线的位置关系,重合/平行/相交(求交点). 直线以其上两点的形式给出(点坐标为整点). 思路 写出直线的一般式方程(用\(gcd\)化为最简), 计算\(\begin{vma ...
- luogu 1355 神秘大三角 判断点和三角形的位置关系 面积法 叉积法
题目链接 题目描述 判断一个点与已知三角形的位置关系. 输入输出格式 输入格式: 前三行:每行一个坐标,表示该三角形的三个顶点 第四行:一个点的坐标,试判断该点与前三个点围成三角形的位置关系 (详见样 ...
- 叉积_判断点与三角形的位置关系 P1355 神秘大三角
题目描述 判断一个点与已知三角形的位置关系. 输入输出格式 输入格式: 前三行:每行一个坐标,表示该三角形的三个顶点 第四行:一个点的坐标,试判断该点与前三个点围成三角形的位置关系 (详见样例) 所有 ...
随机推荐
- HZOI2019SF
Simulation Final 坑.下午我要爆零(RP++) upd: 哈哈哈哈哈哈哈哈哈哈我真的爆零了哈哈哈哈哈哈哈哈哈哈 关于细节, T1A了但是和T3交反了哈哈哈哈哈哈哈哈哈哈 我说我真的不是 ...
- SCP-bzoj-1068
项目编号:bzoj-1068 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 区间DP.f[l][r][s]表示l到r的子串能最小被压成的长度,其中s∈[0,1]表示该串压缩后串中是否能含有M ...
- MVC 中对返回的 data 进行压缩
在webAPI 中返回数据,在数据量比较大的情况的下,返回的data 也可能比较大,有时候可能大于1兆,因此对数据进行压缩能极大的提高数据下载到客户端的时间,提高页面的加载速度. 思路: 在web a ...
- Dubbo入门到精通学习笔记(六):持续集成管理平台之Hudson 持续集成服务器的安装配置与使用
文章目录 安装Hudson 使用Hudson tips:自动化部署 附录:两个脚本 安装Hudson IP:192.168.4.221 8G 内存(Hudson 多个工程在同时构建的情况下比较耗内存) ...
- error C4996: 'getcwd': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getcwd. See online help for details. c:\users\12968\desktop\testapp\testapp\testapp.c
解决办法: 属性>C/C++>预处理器定义>分别输入: _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE >保存退出即可
- Pytest初体验
Pytest安装,导入相关依赖哭 Pip3 install –U pytest U表示升级 Pip3 install pytestsugar pip3 install pytest-rerunfail ...
- 安装Hama的基本过程
- 引用opencv异常
1.异常AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d' 原因:**3.X以后OpenCv只包含部分内容,需要神经网络或者 ...
- html标签注意事项
1,关于媒体的video标签 在同一个页面上如果有多个video标签,并且初始化都赋值,则video不会播放, 解决办法,用计时器每隔50ms给后面的video标签设置src,设置完为止 2,关于ch ...
- redis主从和集群搭建
主从搭建 redis的主从搭建非常简单,打开配置文件6379.conf,只需要将主节点的protected-mode设置为no,然后在从节点配置中加入:slaveof <masterip> ...