当前位置:   article > 正文

Codeforces 166B - Polygon (判断凸包位置关系)

166b.cc

Codeforces Round #113 (Div. 2)

题目链接:Polygons

You've got another geometrical task. You are given two non-degenerate polygons AA and BB as vertex coordinates. Polygon AA is strictly convex. Polygon BB 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 BB is positioned strictly inside polygon AA. It means that any point of polygon BB should be strictly inside polygon AA. "Strictly" means that the vertex of polygon BB cannot lie on the side of the polygon AA.

Input

The first line contains the only integer n(3n105)n(3n105) — the number of vertices of polygon A. Then n lines contain pairs of integers xi,yi(|xi|,|yi|109) — 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(3m2·104) — the number of vertices of polygon B. Then following m lines contain pairs of integers xj,yj(|xj|,|yj|109) — 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

  1. 6
  2. -2 1
  3. 0 3
  4. 3 3
  5. 4 1
  6. 3 -2
  7. 2 -2
  8. 4
  9. 0 1
  10. 2 2
  11. 3 1
  12. 1 0

output

YES

input

  1. 5
  2. 1 2
  3. 4 2
  4. 3 -3
  5. -2 -2
  6. -2 1
  7. 4
  8. 0 1
  9. 1 2
  10. 4 1
  11. 2 -1

output

NO

input

  1. 5
  2. -1 2
  3. 2 3
  4. 4 1
  5. 3 -2
  6. 0 -3
  7. 5
  8. 1 0
  9. 1 1
  10. 3 1
  11. 5 -1
  12. 2 -1

output

NO

Solution

题意

给定两个凸包 AB。判断凸包 B 是否严格在凸包 A 内。

题解

对凸包 AB 的所有点构造凸包,判断该凸包是否等于凸包 A。若相等,则凸包 B 严格在凸包 A 内。

Code

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const double eps = 1e-8;
  4. const double pi = acos(-1.0);
  5. int dcmp(double x) {
  6. if (fabs(x) <= eps)
  7. return 0;
  8. return x > 0 ? 1 : -1;
  9. }
  10. class Point {
  11. public:
  12. double x, y;
  13. Point(double x = 0, double y = 0) : x(x), y(y) {}
  14. Point operator+(Point a) {
  15. return Point(a.x + x, a.y + y);
  16. }
  17. Point operator-(Point a) {
  18. return Point(x - a.x, y - a.y);
  19. }
  20. bool operator<(const Point &a) const {
  21. if (x == a.x)
  22. return y < a.y;
  23. return x < a.x;
  24. }
  25. bool operator==(const Point &a) const {
  26. if (fabs(x - a.x) < eps && fabs(y - a.y) < eps)
  27. return 1;
  28. return 0;
  29. }
  30. bool operator!=(const Point &a) const {
  31. if ((*this) == a)
  32. return 0;
  33. return 1;
  34. }
  35. double length() {
  36. return sqrt(x * x + y * y);
  37. }
  38. };
  39. typedef Point Vector;
  40. double cross(Vector a, Vector b) {
  41. return a.x * b.y - a.y * b.x;
  42. }
  43. double dot(Vector a, Vector b) {
  44. return a.x * b.x + a.y * b.y;
  45. }
  46. bool isclock(Point p0, Point p1, Point p2) {
  47. Vector a = p1 - p0;
  48. Vector b = p2 - p0;
  49. if (dcmp(cross(a, b)) <= 0) // 为了让凸包边上可以有点
  50. return true;
  51. return false;
  52. }
  53. double getDistance(Point a, Point b) {
  54. return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
  55. }
  56. typedef vector<Point> Polygon;
  57. Polygon Andrew(Polygon s) {
  58. Polygon u, l;
  59. if(s.size() < 3) return s;
  60. sort(s.begin(), s.end());
  61. u.push_back(s[0]);
  62. u.push_back(s[1]);
  63. l.push_back(s[s.size() - 1]);
  64. l.push_back(s[s.size() - 2]);
  65. for(int i = 2 ; i < s.size() ; ++i) {
  66. for(int n = u.size() ; n >= 2 && !isclock(u[n - 2], u[n - 1], s[i]); --n) {
  67. u.pop_back();
  68. }
  69. u.push_back(s[i]);
  70. }
  71. for(int i = s.size() - 3 ; i >= 0 ; --i) {
  72. for(int n = l.size() ; n >=2 && !isclock(l[n-2],l[n-1],s[i]); --n) {
  73. l.pop_back();
  74. }
  75. l.push_back(s[i]);
  76. }
  77. for(int i = 1 ; i < u.size() - 1 ; i++) l.push_back(u[i]);
  78. return l;
  79. }
  80. // 判断点在线段上
  81. bool OnSegment(Point p, Point a1, Point a2) {
  82. return dcmp(cross(a1 - p, a2 - p)) == 0 && dcmp(dot(a1 - p, a2 - p)) < 0;
  83. }
  84. // 判断点在凸包内
  85. int isPointInPolygon(Point p, vector<Point> s) {
  86. int wn = 0, cc = s.size();
  87. for (int i = 0; i < cc; i++) {
  88. Point p1 = s[i];
  89. Point p2 = s[(i + 1) % cc];
  90. if (p1 == p || p2 == p || OnSegment(p, p1, p2)) return -1;
  91. int k = dcmp(cross(p2 - p1, p - p1));
  92. int d1 = dcmp(p1.y - p.y);
  93. int d2 = dcmp(p2.y - p.y);
  94. if (k > 0 && d1 <= 0 && d2 > 0) wn++;
  95. if (k < 0 && d2 <= 0 && d1 > 0) wn--;
  96. }
  97. if (wn != 0) return 1;
  98. return 0;
  99. }
  100. int main() {
  101. Polygon A, B;
  102. int n;
  103. scanf("%d", &n);
  104. for (int i = 0; i < n; ++i) {
  105. double x, y;
  106. scanf("%lf%lf", &x, &y);
  107. A.push_back(Point(x, y));
  108. B.push_back(Point(x, y));
  109. }
  110. scanf("%d", &n);
  111. for (int i = 0; i < n; ++i) {
  112. double x, y;
  113. scanf("%lf%lf", &x, &y);
  114. B.push_back(Point(x, y));
  115. }
  116. A = Andrew(A);
  117. B = Andrew(B);
  118. if(A.size() != B.size()) {
  119. printf("NO\n");
  120. return 0;
  121. }
  122. int flag = 1;
  123. sort(A.begin(), A.end());
  124. sort(B.begin(), B.end());
  125. for(int i = 0; i < A.size(); ++i) {
  126. if(A[i] != B[i]) {
  127. flag = 0;
  128. break;
  129. }
  130. }
  131. if(flag) {
  132. printf("YES\n");
  133. } else {
  134. printf("NO\n");
  135. }
  136. return 0;
  137. }

转载于:https://www.cnblogs.com/wulitaotao/p/11421192.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/72595
推荐阅读
相关标签
  

闽ICP备14008679号