Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include<cstdlib>
  2. #include<vector>
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. struct Point
  7. {
  8.     int x, y;
  9. };
  10.  
  11. //trójka - wspóliniowa 0, zgodnie 1, przeciwnie 2
  12. int orientation(Point p, Point q, Point r)
  13. {
  14.     int val = (q.y - p.y) * (r.x - q.x) -
  15.               (q.x - p.x) * (r.y - q.y);
  16.  
  17.     if (val == 0) return 0;
  18.     return (val > 0)? 1: 2;
  19. }
  20.  
  21. void convexHull(Point points[], int n)
  22. {
  23.     if (n < 3) return;
  24.  
  25.     vector<Point> hull;
  26.  
  27.     int l = 0;
  28.     for (int i = 1; i < n; i++)
  29.         if (points[i].x < points[l].x)
  30.             l = i;
  31.             int p = l, q;
  32.     do
  33.     {
  34.         hull.push_back(points[p]);
  35.         q = (p+1)%n;
  36.         for (int i = 0; i < n; i++)
  37.         {
  38.  
  39.             if (orientation(points[p], points[i], points[q]) == 2)
  40.                 q = i;
  41.         }
  42.  
  43.  
  44.         p = q;
  45.  
  46.     } while (p != l);
  47.  
  48.     for (int i = 0; i < hull.size(); i++)
  49.         cout << "(" << hull[i].x << ", "
  50.              << hull[i].y << ")\n";
  51. }
  52.  
  53. int main()
  54. {
  55.     Point points[] = {
  56.             {0, 3},
  57.             {2, 2},
  58.             {-1, -1},
  59.             {2, 1},
  60.             {3, 0},
  61.             {0, 0},
  62.             {4, 4}
  63.     };
  64.     int n = sizeof(points)/sizeof(points[0]);
  65.     convexHull(points, n);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement