Advertisement
ke_timofeeva7

проверка на выпуклость (четырехугольник)

Nov 5th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <memory.h>
  8. #include <stdio.h>
  9. #include <stack>
  10. #include <deque>
  11. #include <queue>
  12. #include <set>
  13. #include <iterator>
  14. #include <map>
  15. #include <iomanip>
  16. #include <unordered_set>
  17. #include <array>
  18. //#define int long long
  19. #define pb push_back
  20. #define fir first
  21. #define sec second
  22. #define double long double
  23. #define endl "\n"
  24. #define un unsigned
  25. //#define INF 1000000000009
  26. //#define EPS 0.000000000001
  27. #define pii pair<int, int>
  28. #define all(v) v.begin(), v.end()
  29. using namespace std;
  30.  
  31. class Point
  32. {
  33. public:
  34.     long long x, y;
  35.     Point(long long x, long long y) : x(x), y(y) {}
  36.     Point() : x(0), y(0) {}
  37. };
  38.  
  39. class Vector
  40. {
  41. public:
  42.     Point a;
  43.  
  44.     Vector(long long x, long long y) : a({ x, y }) {}
  45.     Vector(Point a, Point b) : a({ a.x - b.x, a.y - b.y }) {}
  46. };
  47.  
  48. class Polygon
  49. {
  50. public:
  51.     vector<Point> a;
  52. };
  53.  
  54. Point vec(Point a, Point b)
  55. {
  56.     return Point(b.x - a.x, b.y - a.y);
  57. }
  58.  
  59. long long crossp(Point a, Point b)
  60. {
  61.     return a.x * b.y - b.x * a.y;
  62. }
  63.  
  64. long long dotp(Point a, Point b)
  65. {
  66.     return a.x * b.x + a.y * b.y;
  67. }
  68.  
  69. Point mn;
  70.  
  71. bool cmp(Point a, Point b)
  72. {
  73.     Vector v1(mn, a);
  74.     Vector v2(mn, b);
  75.  
  76.     long long dt1 = crossp(v1.a, v2.a);
  77.  
  78.     if (dt1 != 0)
  79.     {
  80.         return dt1 > 0;
  81.     }
  82.     else
  83.     {
  84.         return dotp(v1.a, v1.a) < dotp(v2.a, v2.a);
  85.     }
  86. }
  87.  
  88. int orientation(Point a, Point b, Point c)
  89. {
  90.     Vector v1(a, b);
  91.     Vector v2(b, c);
  92.  
  93.     long long check = crossp(v1.a, v2.a);
  94.  
  95.     if (check == 0)
  96.     {
  97.         return 0;
  98.     }
  99.     else if (check < 0)
  100.     {
  101.         return -1;
  102.     }
  103.     else
  104.     {
  105.         return 1;
  106.     }
  107. }
  108.  
  109. Polygon convex_hull(Polygon& p)
  110. {
  111.     if (p.a.size() == 1)
  112.     {
  113.         return p;
  114.     }
  115.  
  116.     int min_pos = 0;
  117.  
  118.     for (int i = 1; i < (int)p.a.size(); i++)
  119.     {
  120.         if (p.a[min_pos].y > p.a[i].y)
  121.         {
  122.             min_pos = i;
  123.         }
  124.         else if (p.a[min_pos].y == p.a[i].y && p.a[min_pos].x > p.a[i].x)
  125.         {
  126.             min_pos = i;
  127.         }
  128.     }
  129.  
  130.     swap(p.a[min_pos], p.a[0]);
  131.  
  132.     mn = p.a[0];
  133.  
  134.     sort(all(p.a), cmp);
  135.  
  136.     Polygon hull;
  137.  
  138.     hull.a.push_back(mn);
  139.  
  140.     for (int i = 1; i < (int)p.a.size(); i++)
  141.     {
  142.         Point p0 = p.a[i];
  143.  
  144.         while ((int)hull.a.size() >= 2 && orientation(hull.a[hull.a.size() - 2], hull.a.back(), p0) <= 0)
  145.         {
  146.             hull.a.pop_back();
  147.         }
  148.         if (hull.a.back().x != p0.x || hull.a.back().y != p0.y)
  149.         {
  150.             hull.a.push_back(p0);
  151.         }
  152.     }
  153.  
  154.     return hull;
  155. }
  156.  
  157. signed main()
  158. {
  159.     ios_base::sync_with_stdio(false);
  160.     cin.tie(0);
  161.     cout.tie(0);
  162.  
  163.     int n;
  164.     n = 4;
  165.  
  166.     Polygon p;
  167.  
  168.     p.a.resize(n);
  169.  
  170.     for (int i = 0; i < n; i++)
  171.     {
  172.         cin >> p.a[i].x >> p.a[i].y;
  173.     }
  174.  
  175.     p = convex_hull(p);
  176.  
  177.     if ((int)p.a.size() == 4)
  178.     {
  179.         cout << 1;
  180.     }
  181.     else
  182.     {
  183.         cout << 0;
  184.     }
  185.  
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement