Skip to content

Commit

Permalink
Fix Minkowskisum
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeletWithoutEgg committed Oct 6, 2023
1 parent 25cd319 commit ff157b8
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions codes/Geometry/MinkowskiSum.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// A, B are convex hull rotate to min by (X, Y)
// A, B are strict convex hull rotate to min by (X, Y)
vector<P> Minkowski(vector<P> A, vector<P> B) {
vector<P> C(1, A[0] + B[0]), s1, s2;
const int N = (int)A.size(), M = (int)B.size();
for(int i = 0; i < N; ++i)
s1.pb(A[(i + 1) % N] - A[i]);
for(int i = 0; i < M; i++)
s2.pb(B[(i + 1) % M] - B[i]);
for(int i = 0, j = 0; i < N || j < M;)
if (j >= N || (i < M && cross(s1[i], s2[j]) >= 0))
C.pb(C.back() + s1[i++]);
else
C.pb(C.back() + s2[j++]);
return hull(C), C;
vector<P> sa(N), sb(M), C(N + M + 1);
for (int i = 0; i < N; i++) sa[i] = A[(i+1)%N]-A[i];
for (int i = 0; i < M; i++) sb[i] = B[(i+1)%M]-B[i];
C[0] = A[0] + B[0];
for (int i = 0, j = 0; i < N || j < M; ) {
P e = (j>=M || (i<N && cross(sa[i], sb[j])>=0))
? sa[i++] : sb[j++];
C[i + j] = e;
}
partial_sum(all(C), C.begin()); C.pop_back();
return convex_hull(C); // just to remove colinear
}

0 comments on commit ff157b8

Please sign in to comment.