This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub Luzhiled/comp-geometry
#include "src/real-geometry/point-cloud/convex-hull-with-index.hpp"
#pragma once #include "src/real-geometry/common/size-alias.hpp" #include "src/real-geometry/class/point.hpp" #include "src/real-geometry/class/polygon.hpp" #include "src/real-geometry/compare/compare-x.hpp" #include "src/real-geometry/operation/cross-product.hpp" #include <algorithm> #include <numeric> #include <tuple> #include <utility> #include <vector> namespace geometry { template< typename R > std::pair< polygon<R>, std::vector< usize > > convex_hull_with_index(const points<R> &pts) { usize n = pts.size(); if (n <= 2) { std::vector< usize > idxs(n); std::iota(idxs.begin(), idxs.end(), 0); return {pts, idxs}; } std::vector< std::pair< point<R>, usize > > ps; ps.reserve(n); for (usize i = 0; i < n; i++) { ps.emplace_back(pts[i], i); } auto cmp = [](const std::pair<point<R>, usize> &a, const std::pair<point<R>, usize> &b) { return compare_x(a.first, b.first); }; std::sort(ps.begin(), ps.end(), cmp); std::vector< usize > idxs(2 * n); polygon<R> poly(2 * n); usize k = 0, i = 0; auto check = [&](usize i) { return sign(cross_product<R>(poly[k - 1] - poly[k - 2], ps[i].first - poly[k - 1])) == -1; }; while (i < n) { while (k >= 2 and check(i)) k--; std::tie(poly[k], idxs[k]) = ps[i]; k++; i++; } i = n - 2; usize t = k + 1; while (true) { while (k >= t and check(i)) k--; std::tie(poly[k], idxs[k]) = ps[i]; k++; if (not i) break; i--; } poly.resize(k - 1); idxs.resize(k - 1); return {poly, idxs}; } }
#line 2 "src/real-geometry/point-cloud/convex-hull-with-index.hpp" #line 2 "src/real-geometry/common/size-alias.hpp" #include <cstddef> namespace geometry { using isize = std::ptrdiff_t; using usize = std::size_t; } #line 2 "src/real-geometry/class/point.hpp" #line 2 "src/real-geometry/class/vector.hpp" #include <complex> #include <iostream> namespace geometry { template< typename R > class vec2d : public std::complex< R > { using complex = std::complex< R >; public: using complex::complex; vec2d(const complex &c): complex::complex(c) {} const R x() const { return this->real(); } const R y() const { return this->imag(); } friend vec2d operator*(const vec2d &v, const R &k) { return vec2d(v.x() * k, v.y() * k); } friend vec2d operator*(const R &k, const vec2d &v) { return vec2d(v.x() * k, v.y() * k); } friend std::istream &operator>>(std::istream &is, vec2d &v) { R x, y; is >> x >> y; v = vec2d(x, y); return is; } }; } #line 4 "src/real-geometry/class/point.hpp" #include <vector> namespace geometry { template< typename R > using point = vec2d<R>; template< typename R > using points = std::vector< point< R > >; } #line 2 "src/real-geometry/class/polygon.hpp" #line 4 "src/real-geometry/class/polygon.hpp" #line 6 "src/real-geometry/class/polygon.hpp" namespace geometry { template< typename R > using polygon = std::vector< point<R> >; template< typename R > using polygons = std::vector< polygon<R> >; } #line 2 "src/real-geometry/compare/compare-x.hpp" #line 2 "src/real-geometry/utility/equals/real-number.hpp" #line 2 "src/real-geometry/utility/sign.hpp" #line 2 "src/real-geometry/common/const/eps.hpp" #line 2 "src/real-geometry/common/float-alias.hpp" namespace geometry { using f80 = long double; using f64 = double; } #line 4 "src/real-geometry/common/const/eps.hpp" namespace geometry { inline static f80 &eps() { static f80 EPS = 1e-10; return EPS; } void set_eps(f80 EPS) { eps() = EPS; } } #line 2 "src/real-geometry/numbers/sign.hpp" #line 2 "src/real-geometry/common/int-alias.hpp" namespace geometry { using i32 = int; using i64 = long long; } #line 4 "src/real-geometry/numbers/sign.hpp" namespace geometry::number::sign { constexpr i32 PLUS = +1; constexpr i32 ZERO = 0; constexpr i32 MINUS = -1; } #line 5 "src/real-geometry/utility/sign.hpp" namespace geometry { using namespace geometry::number::sign; template< typename R > inline int sign(R r) { if (r < -eps()) return MINUS; if (r > +eps()) return PLUS; return ZERO; } } #line 4 "src/real-geometry/utility/equals/real-number.hpp" namespace geometry { template< typename R > bool equals(R a, R b) { return sign(a - b) == 0; } } #line 5 "src/real-geometry/compare/compare-x.hpp" namespace geometry { template< typename R > bool compare_x(const point<R> &a, const point<R> &b) { return not equals(a.x(), b.x()) ? a.x() < b.x() : a.y() < b.y(); } } #line 2 "src/real-geometry/operation/cross-product.hpp" #line 4 "src/real-geometry/operation/cross-product.hpp" namespace geometry { template< typename R > R cross_product(const vec2d<R> &a, const vec2d<R> &b) { return a.x() * b.y() - a.y() * b.x(); } } #line 8 "src/real-geometry/point-cloud/convex-hull-with-index.hpp" #include <algorithm> #include <numeric> #include <tuple> #include <utility> #line 14 "src/real-geometry/point-cloud/convex-hull-with-index.hpp" namespace geometry { template< typename R > std::pair< polygon<R>, std::vector< usize > > convex_hull_with_index(const points<R> &pts) { usize n = pts.size(); if (n <= 2) { std::vector< usize > idxs(n); std::iota(idxs.begin(), idxs.end(), 0); return {pts, idxs}; } std::vector< std::pair< point<R>, usize > > ps; ps.reserve(n); for (usize i = 0; i < n; i++) { ps.emplace_back(pts[i], i); } auto cmp = [](const std::pair<point<R>, usize> &a, const std::pair<point<R>, usize> &b) { return compare_x(a.first, b.first); }; std::sort(ps.begin(), ps.end(), cmp); std::vector< usize > idxs(2 * n); polygon<R> poly(2 * n); usize k = 0, i = 0; auto check = [&](usize i) { return sign(cross_product<R>(poly[k - 1] - poly[k - 2], ps[i].first - poly[k - 1])) == -1; }; while (i < n) { while (k >= 2 and check(i)) k--; std::tie(poly[k], idxs[k]) = ps[i]; k++; i++; } i = n - 2; usize t = k + 1; while (true) { while (k >= t and check(i)) k--; std::tie(poly[k], idxs[k]) = ps[i]; k++; if (not i) break; i--; } poly.resize(k - 1); idxs.resize(k - 1); return {poly, idxs}; } }