Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct Point { int x, y, num; } P[512], CH[512];
- double cross(Point o, Point a, Point b)
- {
- return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
- }
- bool compare(Point a, Point b)
- {
- return (a.y < b.y) || (a.y == b.y && a.x < b.x);
- }
- int Andrew_monotone_chain(int pointnum)
- {
- // 將所有點依照座標大小排序
- sort(P, P + pointnum, compare);
- int m = 0; // m 為凸包頂點數目
- // 包下半部
- for (int i = 0; i < pointnum; ++i)
- {
- while (m >= 2 && cross(CH[m - 2], CH[m - 1], P[i]) <= 0) m--;
- CH[m++] = P[i];
- }
- // 包上半部,不用再包入方才包過的終點,但會再包一次起點
- for (int i = pointnum - 2, t = m + 1; i >= 0; --i)
- {
- while (m >= t && cross(CH[m - 2], CH[m - 1], P[i]) <= 0) m--;
- CH[m++] = P[i];
- }
- return m;
- }
- int main()
- {
- int set, pointnum, convexhullnum, end;
- cin >> set;
- cout << set << endl;
- for (int i = 0; i < set; i++)
- {
- cin >> pointnum;
- for (int j = 0; j < pointnum; j++)
- {
- cin >> P[j].x;
- cin >> P[j].y;
- P[j].num = j;
- }
- if (i != set - 1)
- cin >> end;
- convexhullnum = Andrew_monotone_chain(pointnum);
- cout << convexhullnum << endl;
- for (int j = 0; j < convexhullnum; j++)
- {
- cout << CH[j].x << " ";
- cout << CH[j].y << endl;
- }
- if (i != set - 1)
- cout << "-1" << endl;
- }
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment