Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/aoj-cgl-7-h.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H"
#define ERROR 0.00001

#include "../../template/template.hpp"

#include "../../geometry/common_area_cp.hpp"

using namespace geometry;

int main() {
  int N;
  Real R;
  cin >> N >> R;
  Circle c(Point(0, 0), R);
  Polygon p(N);
  for(auto &s : p) cin >> s;
  cout << common_area_cp(c, p) << endl;
}
#line 1 "test/verify/aoj-cgl-7-h.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H"
#define ERROR 0.00001

#line 1 "template/template.hpp"
#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};
 
template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
#line 5 "test/verify/aoj-cgl-7-h.test.cpp"

#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 3 "geometry/line.hpp"

namespace geometry {
  struct Line {
    Point a, b;

    Line() = default;

    Line(const Point &a, const Point &b) : a(a), b(b) {}

    Line(const Real &A, const Real &B, const Real &C) { // Ax+By=C
      if(equals(A, 0)) {
        assert(!equals(B, 0));
        a = Point(0, C / B);
        b = Point(1, C / B);
      } else if(equals(B, 0)) {
        a = Point(C / A, 0);
        b = Point(C / A, 1);
      } else {
        a = Point(0, C / B);
        b = Point(C / A, 0);
      }
    }

    friend ostream &operator<<(ostream &os, Line &l) {
      return os << l.a << " to " << l.b;
    }

    friend istream &operator>>(istream &is, Line &l) {
      return is >> l.a >> l.b;
    }
  };

  using Lines = vector< Line >;
}
#line 3 "geometry/segment.hpp"

namespace geometry {
  struct Segment : Line {
    Segment() = default;

    using Line::Line;
  };

  using Segments = vector< Segment >;
}
#line 2 "geometry/projection.hpp"

#line 5 "geometry/projection.hpp"

namespace geometry {
  // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A
  Point projection(const Line &l, const Point &p) {
    auto t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
    return l.a + (l.a - l.b) * t;
  }
}
#line 3 "geometry/ccw.hpp"

namespace geometry {
  // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C
  constexpr int COUNTER_CLOCKWISE = +1;
  constexpr int CLOCKWISE = -1;
  constexpr int ONLINE_BACK = +2; // c-a-b
  constexpr int ONLINE_FRONT = -2; // a-b-c
  constexpr int ON_SEGMENT = 0; // a-c-b
  int ccw(const Point &a, Point b, Point c) {
    b = b - a, c = c - a;
    if(sign(cross(b, c)) == +1) return COUNTER_CLOCKWISE;
    if(sign(cross(b, c)) == -1) return CLOCKWISE;
    if(sign(dot(b, c)) == -1) return ONLINE_BACK;
    if(norm(b) < norm(c)) return ONLINE_FRONT;
    return ON_SEGMENT;
  }
}
#line 4 "geometry/is_intersect_sp.hpp"

namespace geometry {
  bool is_intersect_sp(const Segment &s, const Point &p) {
    return ccw(s.a, s.b, p) == ON_SEGMENT;
  }
}
#line 5 "geometry/distance_sp.hpp"

namespace geometry {
  Real distance_sp(const Segment &s, const Point &p) {
    Point r = projection(s, p);
    if(is_intersect_sp(s, r)) return abs(r - p);
    return min(abs(s.a - p), abs(s.b - p));
  }
}
#line 3 "geometry/circle.hpp"

namespace geometry {
  struct Circle {
    Point p;
    Real r{};

    Circle() = default;

    Circle(const Point &p, const Real &r) : p(p), r(r) {}
  };

  using Circles = vector< Circle >;
}
#line 6 "geometry/cross_point_cl.hpp"

namespace geometry {
  // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D
  Points cross_point_cl(const Circle &c, const Line &l) {
    Point pr = projection(l, c.p);
    if(equals(abs(pr - c.p), c.r)) return {pr};
    Point e = (l.b - l.a) / abs(l.b - l.a);
    auto k = sqrt(norm(c.r) - norm(pr - c.p));
    return {pr - e * k, pr + e * k};
  }
}
#line 6 "geometry/is_intersect_cs.hpp"

namespace geometry {
  int is_intersect_cs(const Circle &c, const Segment &l) {
    Point h = projection(l, c.p);
    if(sign(norm(h - c.p) - norm(c.r)) > 0) return 0;
    auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);
    if(sign(c.r - d1) >= 0 && sign(c.r - d2) >= 0) return 0;
    if(sign(c.r - d1) < 0 && sign(d2 - c.r) > 0 || sign(d1 - c.r) > 0 && sign(c.r - d2) < 0) return 1;
    if(sign(dot(l.a - h, l.b - h)) < 0) return 2;
    return 0;
  }
}
#line 7 "geometry/common_area_cp.hpp"

namespace geometry {
  // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H
  Real ca_cp_impl(const Circle &c, const Point &a, const Point &b) {
    auto va = c.p - a, vb = c.p - b;
    Real f = cross(va, vb), ret = 0;
    if(sign(f) == 0) return ret;
    if(sign(max(abs(va), abs(vb)) - c.r) <= 0) return f;
    if(sign(distance_sp(Segment(a, b), c.p) - c.r) >= 0) return norm(c.r) * arg(vb * conj(va));
    auto tot = cross_point_cl(c, Line(a, b));
    if(is_intersect_cs(c, Segment(a, b)) != 2 and dot(a - tot[0], b - tot[0]) < 0) {
      swap(tot[0], tot[1]);
    }
    tot.emplace(begin(tot), a);
    tot.emplace_back(b);
    for(int i = 1; i < (int) tot.size(); i++) {
      ret += ca_cp_impl(c, tot[i - 1], tot[i]);
    }
    return ret;
  }

  Real common_area_cp(const Circle &c, const Polygon &p) {
    if(p.size() < 3) return 0;
    Real A = 0;
    for(int i = 0; i < p.size(); i++) {
      A += ca_cp_impl(c, p[i], p[(i + 1) % p.size()]);
    }
    return A * 0.5;
  }
}
#line 7 "test/verify/aoj-cgl-7-h.test.cpp"

using namespace geometry;

int main() {
  int N;
  Real R;
  cin >> N >> R;
  Circle c(Point(0, 0), R);
  Polygon p(N);
  for(auto &s : p) cin >> s;
  cout << common_area_cp(c, p) << endl;
}
Back to top page