diff options
| author | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
|---|---|---|
| committer | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
| commit | 5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch) | |
| tree | ed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /geometry/convexHull.cpp | |
| parent | adabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff) | |
updated tcr
Diffstat (limited to 'geometry/convexHull.cpp')
| -rw-r--r-- | geometry/convexHull.cpp | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/geometry/convexHull.cpp b/geometry/convexHull.cpp index 9c14c45..e988977 100644 --- a/geometry/convexHull.cpp +++ b/geometry/convexHull.cpp @@ -1,24 +1,19 @@ -// Laufzeit: O(n*log(n)) -ll cross(const pt p, const pt a, const pt b) { - return (a.x - p.x) * (b.y - p.y) - (a.y - p.y) * (b.x - p.x); -} - -// Punkte auf der konvexen Hülle, gegen den Uhrzeigersinn sortiert. -// Kollineare Punkte nicht enthalten, entferne dafür "=" im CCW-Test. -// Achtung: Der erste und letzte Punkt im Ergebnis sind gleich. -// Achtung: Alle Punkte müssen verschieden sein. vector<pt> convexHull(vector<pt> p){ - int n = p.size(), k = 0; - vector<pt> h(2 * n); - sort(p.begin(), p.end()); - for (int i = 0; i < n; i++) { // Untere Hülle. - while (k >= 2 && cross(h[k - 2], h[k - 1], p[i]) <= 0.0) k--; - h[k++] = p[i]; - } - for (int i = n - 2, t = k + 1; i >= 0; i--) { // Obere Hülle. - while (k >= t && cross(h[k - 2], h[k - 1], p[i]) <= 0.0) k--; - h[k++] = p[i]; - } - h.resize(k); - return h; + sort(p.begin(), p.end(), [](const pt& a, const pt& b){ + return real(a) == real(b) ? imag(a) < imag(b) + : real(a) < real(b); + }); + p.erase(unique(p.begin(), p.end()), p.end()); + int k = 0; + vector<pt> h(2 * sz(p)); + for (int i = 0; i < sz(p); i++) {// Untere Hülle. + while (k > 1 && cross(h[k-2], h[k-1], p[i]) <= 0) k--; + h[k++] = p[i]; + } + for (int i = sz(p)-2, t = k; i >= 0; i--) {// Obere Hülle. + while (k > t && cross(h[k-2], h[k-1], p[i]) <= 0) k--; + h[k++] = p[i]; + } + h.resize(k); + return h; } |
