Problem: Find the area of intersection between a circle $c$ and a polygon $g$. The center coordinate of the circle is ($0, 0$).

The polygon $g$ is represented by a sequence of points $p_1$, $p_2$,..., $p_n$ where line segments connecting $p_i$ and $p_{i+1}$ ($1 \leq i \leq n−1$) are sides of the polygon. The line segment connecting $p_n$ and $p_1$ is also a side of the polygon.

Note that the polygon is not necessarily convex.

Constraints

* $3 \leq n \leq 100$
* $1 \leq r \leq 100$
* $-100 \leq x_i, y_i \leq 100$

Input

The input is given in the following format.

$n$ $r$
$x_1$ $y_1$
$x_2$ $y_2$
:
$x_n$ $y_n$


In the first line, an integer n representing the number of points in the polygon is given. The coordinate of a point $p_i$ is given by two integers $x_i$ and $y_i$. The coordinates of the points are given in the order of counter-clockwise visit of them. All input values are given in integers.

Output

Print the area of intersection in a line. The output values should be in a decimal fraction with an error less than 0.00001.

Examples

Input

3 5
1 1
4 1
5 5


Output

4.639858417607


Input

4 5
0 0
-3 -6
1 -3
5 -4


Output

11.787686807576

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;

template<class T> using V = vector<T>;

using D = double;
const D PI = acos(D(-1)), EPS = 1e-10;

int sgn(D a) { return (abs(a) <= EPS) ? 0 : (a < 0 ? -1 : 1); }
int sgn(D a, D b) { return sgn(a-b); }
//relative sign
// int rsgn(D a, D f) {
//     if (abs(a) <= f*EPS) return 0;
//     return (a < 0) ? -1 : 1;
// }
struct Pt2 {
    D x, y;
    Pt2(D _x = D(), D _y = D()) : x(_x), y(_y) {}
    Pt2 operator+(const Pt2 &r) const { return Pt2(x+r.x, y+r.y); }
    Pt2 operator-(const Pt2 &r) const { return Pt2(x-r.x, y-r.y); }
    Pt2 operator*(const Pt2 &r) const { return Pt2(x*r.x-y*r.y, x*r.y+y*r.x); }

    Pt2 operator*(const D &r) const { return Pt2(x*r, y*r); }
    Pt2 operator/(const D &r) const { return Pt2(x/r, y/r); }

    Pt2& operator+=(const Pt2 &r) { return *this=*this+r; }
    Pt2& operator-=(const Pt2 &r) { return *this=*this-r; }
    Pt2& operator*=(const Pt2 &r) { return *this=*this*r; }
    Pt2& operator*=(const D &r) { return *this=*this*r; }
    Pt2& operator/=(const D &r) { return *this=*this/r; }
    
    Pt2 operator-() const { return Pt2(-x, -y); }

    bool operator<(const Pt2 &r) const { return 2*sgn(x, r.x)+sgn(y, r.y)<0; }
    bool operator==(const Pt2 &r) const { return sgn((*this-r).rabs()) == 0; }

    D norm() const { return x*x + y*y; }
    D abs() const { return sqrt(norm()); }
    D rabs() const { return max(std::abs(x), std::abs(y)); } // robust abs
    D arg() const { return atan2(y, x); }

    pair<D, D> to_pair() const { return make_pair(x, y); }
    static Pt2 polar(D le, D th) { return Pt2(le*cos(th), le*sin(th)); }
};
ostream& operator<<(ostream& os, const Pt2 &p) {
    return os << "P(" << p.x << ", " << p.y << ")";
}
using P = Pt2;

struct L {
    P s, t;
    L(P _s = P(), P _t = P()) : s(_s), t(_t) {}
    P vec() const { return t-s; }
    D abs() const { return vec().abs(); }
    D arg() const { return vec().arg(); }
};
ostream& operator<<(ostream& os, const L &l) {
    return os << "L(" << l.s << ", " << l.t << ")";
}

D cross(P a, P b) { return a.x*b.y - a.y*b.x; }
D dot(P a, P b) { return a.x*b.x + a.y*b.y; }
// cross(a, b) is too small?
int sgncrs(P a, P b) {
    D cr = cross(a, b);
    if (abs(cr) <= (a.rabs() + b.rabs()) * EPS) return 0;
    return (cr < 0) ? -1 : 1;
}

// -2, -1, 0, 1, 2 : front, clock, on, cclock, back
int ccw(P b, P c) {
    int s = sgncrs(b, c);
    if (s) return s;
    if (!sgn(c.rabs()) || !sgn((c-b).rabs())) return 0;
    if (dot(b, c) < 0) return 2;
    if (dot(-b, c-b) < 0) return -2;
    return 0;
}
int ccw(P a, P b, P c) { return ccw(b-a, c-a); }
int ccw(L l, P p) { return ccw(l.s, l.t, p); }

P project(const L &l, const P &p) {
    P v = l.vec();
    return l.s + v * (dot(v, p-l.s) / v.norm());
}

bool insSL(const L &s, const L &l) {
    int a = ccw(l, s.s), b = ccw(l, s.t);
    return (a%2 == 0 || b%2 == 0 || a != b);
}

bool insSS(const L &s, const L &t) {
    int a = ccw(s, t.s), b = ccw(s, t.t);
    int c = ccw(t, s.s), d = ccw(t, s.t);
    if (a*b <= 0 && c*d <= 0) return true;
    return false;
}

D distLP(const L &l, const P &p) {
    return abs(cross(l.vec(), p-l.s)) / l.abs();
}

D distSP(const L &s, const P &p) {
    P q = project(s, p);
    if (ccw(s, q) == 0) return (p - q).abs();
    else return min((s.s - p).abs(), (s.t - p).abs());
}

using Pol = V<P>;

struct C {
    P p; D r;
    C(P _p = P(), D _r = D()) : p(_p), r(_r) {}
};

//need Intersect/distLP, r.sはよりl.sに近い
int crossCL(const C &c, const L &l, L &r) {
    D u = distLP(l, c.p);
    int si = sgn(u, c.r);    
    if (si == 1) return 0;
    P v = project(l, c.p);
    P di = (si == 0) ? P(0, 0) : l.vec() * (sqrt(c.r*c.r - u*u) / l.abs());
    r = L(v-di, v+di);
    if (si == 0) return 1;
    return 2;
}

// Tri((0, 0), a, b)とC(P(0, 0), r)の共有面積
D insArea2TC(const P &a, const P &b, const D &r) {
    if (a == b) return 0;
    auto single = [&](P x, P y, bool tri) {
        if (tri) return cross(x, y);
        else return r * r * ((y * P(x.x, -x.y)).arg());
    };
    bool ins_a = sgn(a.abs(), r) != 1, ins_b = sgn(b.abs(), r) != 1;
    if (ins_a && ins_b) return single(a, b, true);
//    if (sgn(max(a.abs(), b.abs()), r) != 1) return cross(a, b);
//    auto sec_s = [&](P x, P y) { return r * r * ((y * P(x.x, -x.y)).arg()); };
//    if (u == 0) { return sec_s(a, b); }
    D r2 = distSP(L(a, b), P(0, 0));    
    if (sgn(r, r2) != 1) return single(a, b, false);
    L l;
    assert(crossCL(C(P(0, 0), r), L(a, b), l) == 2);
    if (ins_a) l.s = l.t;
    else if (ins_b) l.t = l.s;
    assert(ccw(a, b, l.s) == 0);
    assert(ccw(a, b, l.t) == 0);
//    swap(l.s, l.t);
    return single(a, l.s, ins_a) + single(l.s, l.t, true) + single(l.t, b, ins_b); //insArea2TC(a, (a+b)/2, r) + insArea2TC((a+b)/2, b, r);
//    assert(ccw(a, b, l.s) == 0);
//    assert(ccw(a, b, l.t) == 0);
//    return sec_s(a, l.t) + cross(l.t, l.s) + sec_s(l.s, b);
//    return insArea2TC(a, (a+b)/2, r) + insArea2TC((a+b)/2, b, r);
}

// p, cの共有面積
D insAreaPolC(const Pol &po, const C &c) {
    D sm = 0;
    P a, b = po.back();
    for (auto p: po) {
        a = b; b = p;
        sm += insArea2TC(a, b, c.r);
    }
    return sm / 2;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;
    
    int n; D r;
    cin >> n >> r;
    C c = C(P(0, 0), r);
    Pol po(n);
    for (int i = 0; i < n; i++) {
        D x, y;
        cin >> x >> y;
        po[i] = P(x, y);
    }
    cout << insAreaPolC(po, c) << endl;
    return 0;
}