Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdlib>
- #include<vector>
- #include<iostream>
- using namespace std;
- struct Point
- {
- int x, y;
- };
- //trójka - wspóliniowa 0, zgodnie 1, przeciwnie 2
- int orientation(Point p, Point q, Point r)
- {
- int val = (q.y - p.y) * (r.x - q.x) -
- (q.x - p.x) * (r.y - q.y);
- if (val == 0) return 0;
- return (val > 0)? 1: 2;
- }
- void convexHull(Point points[], int n)
- {
- if (n < 3) return;
- vector<Point> hull;
- int l = 0;
- for (int i = 1; i < n; i++)
- if (points[i].x < points[l].x)
- l = i;
- int p = l, q;
- do
- {
- hull.push_back(points[p]);
- q = (p+1)%n;
- for (int i = 0; i < n; i++)
- {
- if (orientation(points[p], points[i], points[q]) == 2)
- q = i;
- }
- p = q;
- } while (p != l);
- for (int i = 0; i < hull.size(); i++)
- cout << "(" << hull[i].x << ", "
- << hull[i].y << ")\n";
- }
- int main()
- {
- Point points[] = {
- {0, 3},
- {2, 2},
- {-1, -1},
- {2, 1},
- {3, 0},
- {0, 0},
- {4, 4}
- };
- int n = sizeof(points)/sizeof(points[0]);
- convexHull(points, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement