AlexNeagu11

Rotating Calipers

Mar 19th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. struct point {
  5. double x, y;
  6. void read() {
  7. cin >> x >> y;
  8. }
  9. void write() {
  10. cout << fixed << setprecision(12) << x << ' ' << y << '\n';
  11. }
  12. point operator - (const point &B) const {
  13. return point{x - B.x, y - B.y};
  14. }
  15. void operator -= (const point &B) {
  16. x -= B.x;
  17. y -= B.y;
  18. }
  19. long double operator * (const point &B) const {
  20. return x * B.y - y * B.x;
  21. }
  22. long double orientation(const point &A, const point &B) const {
  23. return (A - *this) * (B - *this);
  24. }
  25. long double dist(point A) {
  26. A -= *this;
  27. return sqrt(A.x * A.x + A.y * A.y);
  28. }
  29. bool operator <(const point &A) {
  30. if(x == A.x) {
  31. return y < A.y;
  32. }
  33. return x < A.x;
  34. }
  35.  
  36. };
  37. vector<point> buildHull(vector<point> A) {
  38. //
  39. vector<point> hull;
  40. for(int i = 0; i < (int) A.size(); ++i) {
  41. while(hull.size() >= 2) {
  42. int sz = hull.size();
  43. point P1 = hull[sz - 2];
  44. point P2 = hull[sz - 1];
  45. if(P2.orientation(P1, A[i]) < 0) {
  46. break;
  47. }
  48. hull.pop_back();
  49. }
  50. hull.push_back(A[i]);
  51. }
  52. return hull;
  53. }
  54.  
  55. int32_t main() {
  56.  
  57. int n;
  58. cin >> n;
  59. vector<point> points(n);
  60. for(int i = 0; i < n; ++i) {
  61. points[i].read();
  62. }
  63.  
  64. sort(points.begin(), points.end());
  65. vector<point> upperHull = buildHull(points);
  66. reverse(points.begin(), points.end());
  67. vector<point> lowerHull = buildHull(points);
  68.  
  69. upperHull.pop_back();
  70. lowerHull.pop_back();
  71. vector<point> polygon;
  72. for(point p : upperHull) {
  73. polygon.push_back(p);
  74. }
  75. for(point p : lowerHull) {
  76. polygon.push_back(p);
  77. }
  78. vector<pair<int,int>> opuse;
  79. int P = 0, size = polygon.size();
  80. for(int i = 0; i < size; ++i) {
  81. while(polygon[i].dist(polygon[P]) <= polygon[i].dist(polygon[(P + 1) % size]) && (P + 1) % size != i) {
  82. P = (P + 1) % size;
  83. }
  84. opuse.push_back(make_pair(i, P));
  85. }
  86. long double ans = 0;
  87. for(auto it : opuse) {
  88. ans = max(ans, polygon[it.first].dist(polygon[it.second]));
  89. }
  90. cout << fixed << setprecision(10) << ans << '\n';
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment