Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: geometry/convex_hull.hpp

Depends on

Verified with

Code

#include "base.hpp"
#include "point.hpp"
#include "polygon.hpp"

namespace geometry {
  // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A
  Polygon convex_hull(Polygon &p, bool strict = true) {
    int n = (int) p.size(), k = 0;
    if(n <= 2) return p;
    sort(begin(p), end(p), compare_x);
    vector< Point > ch(2 * n);
    auto check = [&](int i) {
      return sign(cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) <= -1 + strict;
    };
    for(int i = 0; i < n; ch[k++] = p[i++]) {
      while(k >= 2 && check(i)) --k;
    }
    for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {
      while(k >= t && check(i)) --k;
    }
    ch.resize(k - 1);
    return ch;
  }
}
#line 2 "geometry/base.hpp"

namespace geometry {
  using Real = double;
  const Real EPS = 1e-8;
  const Real PI = acos(static_cast< Real >(-1));

  enum {
    OUT, ON, IN
  };

  inline int sign(const Real &r) {
    return r <= -EPS ? -1 : r >= EPS ? 1 : 0;
  }

  inline bool equals(const Real &a, const Real &b) {
    return sign(a - b) == 0;
  }
}
#line 3 "geometry/point.hpp"

namespace geometry {
  using Point = complex< Real >;

  istream &operator>>(istream &is, Point &p) {
    Real a, b;
    is >> a >> b;
    p = Point(a, b);
    return is;
  }

  ostream &operator<<(ostream &os, const Point &p) {
    return os << real(p) << " " << imag(p);
  }

  Point operator*(const Point &p, const Real &d) {
    return Point(real(p) * d, imag(p) * d);
  }

  // rotate point p counterclockwise by theta rad
  Point rotate(Real theta, const Point &p) {
    return Point(cos(theta) * real(p) - sin(theta) * imag(p), sin(theta) * real(p) + cos(theta) * imag(p));
  }

  Real cross(const Point &a, const Point &b) {
    return real(a) * imag(b) - imag(a) * real(b);
  }

  Real dot(const Point &a, const Point &b) {
    return real(a) * real(b) + imag(a) * imag(b);
  }

  bool compare_x(const Point &a, const Point &b) {
    return equals(real(a), real(b)) ? imag(a) < imag(b) : real(a) < real(b);
  }

  bool compare_y(const Point &a, const Point &b) {
    return equals(imag(a), imag(b)) ? real(a) < real(b) : imag(a) < imag(b);
  }

  using Points = vector< Point >;
}
#line 2 "geometry/polygon.hpp"

#line 4 "geometry/polygon.hpp"

namespace geometry {
  using Polygon = vector< Point >;
  using Polygons = vector< Polygon >;
}
#line 4 "geometry/convex_hull.hpp"

namespace geometry {
  // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A
  Polygon convex_hull(Polygon &p, bool strict = true) {
    int n = (int) p.size(), k = 0;
    if(n <= 2) return p;
    sort(begin(p), end(p), compare_x);
    vector< Point > ch(2 * n);
    auto check = [&](int i) {
      return sign(cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) <= -1 + strict;
    };
    for(int i = 0; i < n; ch[k++] = p[i++]) {
      while(k >= 2 && check(i)) --k;
    }
    for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {
      while(k >= t && check(i)) --k;
    }
    ch.resize(k - 1);
    return ch;
  }
}
Back to top page