Guest User

Untitled

a guest
Jun 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Point { int x, y, num; } P[512], CH[512];
  8.  
  9. double cross(Point o, Point a, Point b)
  10. {
  11. return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
  12. }
  13.  
  14. bool compare(Point a, Point b)
  15. {
  16. return (a.y < b.y) || (a.y == b.y && a.x < b.x);
  17. }
  18.  
  19.  
  20. int Andrew_monotone_chain(int pointnum)
  21. {
  22. // 將所有點依照座標大小排序
  23. sort(P, P + pointnum, compare);
  24.  
  25. int m = 0; // m 為凸包頂點數目
  26.  
  27. // 包下半部
  28. for (int i = 0; i < pointnum; ++i)
  29. {
  30. while (m >= 2 && cross(CH[m - 2], CH[m - 1], P[i]) <= 0) m--;
  31. CH[m++] = P[i];
  32. }
  33.  
  34. // 包上半部,不用再包入方才包過的終點,但會再包一次起點
  35. for (int i = pointnum - 2, t = m + 1; i >= 0; --i)
  36. {
  37. while (m >= t && cross(CH[m - 2], CH[m - 1], P[i]) <= 0) m--;
  38. CH[m++] = P[i];
  39. }
  40.  
  41. return m;
  42. }
  43.  
  44. int main()
  45. {
  46. int set, pointnum, convexhullnum, end;
  47.  
  48. cin >> set;
  49. cout << set << endl;
  50. for (int i = 0; i < set; i++)
  51. {
  52. cin >> pointnum;
  53. for (int j = 0; j < pointnum; j++)
  54. {
  55. cin >> P[j].x;
  56. cin >> P[j].y;
  57. P[j].num = j;
  58. }
  59.  
  60. if (i != set - 1)
  61. cin >> end;
  62.  
  63. convexhullnum = Andrew_monotone_chain(pointnum);
  64.  
  65. cout << convexhullnum << endl;
  66. for (int j = 0; j < convexhullnum; j++)
  67. {
  68. cout << CH[j].x << " ";
  69. cout << CH[j].y << endl;
  70. }
  71.  
  72. if (i != set - 1)
  73. cout << "-1" << endl;
  74. }
  75.  
  76. system("pause");
  77. return 0;
  78. }
Add Comment
Please, Sign In to add comment