Advertisement
Mlxa

Реализация суммы Минковского

Mar 2nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. vector<Vector> prepare(vector<Vector> a) {
  2.   int n = (int)a.size();
  3.   int i = 0;
  4.   for (int j = 1; j < n; ++j) {
  5.     if (make_pair(a[j].y, -a[j].x) > make_pair(a[i].y, -a[i].x)) {
  6.       i = j;
  7.     }
  8.   }
  9.   rotate(a.begin(), a.begin() + i, a.end());
  10.   a.push_back(a[0]);
  11.   a.push_back(a[1]);
  12.   return a;
  13. }
  14.  
  15. double angle(Vector a) {
  16.   if (a.y == 0 && a.x == 0) {
  17.     assert(false);
  18.   }
  19.   double ans = atan2l(a.y, a.x);
  20.   return ans;
  21. }
  22.  
  23. vector<Vector> sum(vector<Vector> a, vector<Vector> b) {
  24.   int n = (int)a.size(), m = (int)b.size();
  25.   assert(n && m);
  26.   a = prepare(a);
  27.   b = prepare(b);
  28.  
  29.   vector<Vector> answer;
  30.   while (i < n || j < m) {
  31.     answer.push_back(a[i] + b[j]);
  32.     if (i >= n) {
  33.       ++j;
  34.       continue;
  35.     }
  36.     if (j >= m) {
  37.       ++i;
  38.       continue;
  39.     }
  40.     if (angle(vec(a[i], a[i + 1])) < angle(vec(b[j], b[j + 1]))) {
  41.       ++i;
  42.     } else if (angle(vec(a[i], a[i + 1])) > angle(vec(b[j], b[j + 1]))) {
  43.       ++j;
  44.     } else {
  45.       assert(i < n && j < m);
  46.       ++i;
  47.       ++j;
  48.     }
  49.   }
  50.   return answer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement