Language: C++

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<climits>
#include<cstring>
#include<queue>
#include<complex>
using namespace std;

const double EPS = 1e-8;

#define curr(G, i) G[i%n]
#define next(G, i) G[(i+1)%n]

typedef complex<double> P;
typedef vector<P> triangle;
typedef vector<P> Poly;
struct L : public vector<P>{
    L(const P &a, const P &b){
        push_back(a), push_back(b);
    }
};

namespace std {
  bool operator < (const P& a, const P& b) {
    return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b);
  }
}

double cross(const P &a, const P &b){
    return imag(conj(a)*b);
}

double dot(const P &a, const P &b){
    return real(conj(a)*b);
}

int ccw(P a, P b, P c){
    b -= a;
    c -= a;
    if(cross(b,c) > 0) return +1;
    if(cross(b,c) < 0) return -1;
    if(dot(b,c) < 0) return +2;
    if(norm(b) < norm(c)) return -2;
    return 0;
}

triangle makeTriangle(const P &a, const P &b, const P &c){
    triangle res(3);
    res[0] = a;
    res[1] = b;
    res[2] = c;
    return res;
}

bool containment(const triangle &tri, const P &p){
    return ccw(tri[0], tri[1], p) >=0 && ccw(tri[1], tri[2], p) >=0 && ccw(tri[2], tri[0], p) >=0;
}

bool triangeJudge(int i, int j, int k, const Poly &G){
    if(ccw(G[i], G[j], G[k])<=0) return false;
    triangle tri = makeTriangle(G[i], G[j], G[k]);
    for (int l=0;l<G.size();l++){
        if(l != i && l != j && l != k){
            if(containment(tri, G[l])) return false;
        }
    }
    return true;
}

vector<triangle> triangulation(Poly &G){
    vector<triangle> res;
    int n = G.size();
    vector<int> l, r;
    for (int i=0;i<n;i++){
        l.push_back((i-1+n)%n);
        r.push_back((i+1+n)%n);
    }

    int i = n-1;
    while(res.size() < n-2){
        i = r[i];
        if(triangeJudge(l[i], i, r[i], G)){
            res.push_back(makeTriangle(G[l[i]], G[i], G[r[i]]));
            l[ r[i] ] = l[i];
            r[ l[i] ] = r[i];
        }
    }
    return res;
}

double area(vector<P> &G){
    int n = G.size();
    double S = 0;
    for (int i=0;i<n;i++){
        P p = curr(G, i);
        P q = next(G, i);
        S += (1.0/2)*cross(p, q);
    }
    return S;
}

double arcArea(P &p, P &q, double r){
    double theta = arg(q) - arg(p);
    if(theta < 0) theta += 2.0 * M_PI;
    return (1.0/2)*r*r*(theta - sin(theta));
}

vector<P> crossCircleLine(P p1, P p2, double r){
    vector<P> res;

    double a = p2.imag() - p1.imag();
    double b = -(p2.real() - p1.real());
    double c = p1.real()*p2.imag() - p2.real()*p1.imag();
    if ( ((a*a + b*b)*r*r - c*c) < 0 )  return res;
    double x1 = (a*c - b*sqrt((a*a + b*b)*r*r - c*c))/(a*a + b*b);
    double y1 = (b*c + a*sqrt((a*a + b*b)*r*r - c*c))/(a*a + b*b);
    double x2 = (a*c + b*sqrt((a*a + b*b)*r*r - c*c))/(a*a + b*b);
    double y2 = (b*c - a*sqrt((a*a + b*b)*r*r - c*c))/(a*a + b*b);
    P q1(x1, y1);
    P q2(x2, y2);

    if (norm(q1-p1) > norm(q2-p1)) swap(q1, q2);
    if (dot(q1- p1, q1 - p2) <= -EPS) res.push_back(q1);
    if (dot(q2- p1, q2 - p2) <= -EPS) res.push_back(q2);

    return res;
}

double intersectionCicleTriangle(triangle &T, double r){
    bool crossing = false;
    for(int i=0;i<3;i++){
        vector<P> cross = crossCircleLine(T[i], T[(i+1)%3], r);
        if (cross.size() != 0){
            crossing = true;
            break;
        }
    }
    if(!crossing && norm(T[0]) > r*r + EPS && containment(T, z)) return M_PI*r*r;


    vector<P> Time;
    vector<P> Arc;
    vector<bool> type;
    bool inOnCircle = (norm(T[0]) <= r*r);
    for(int i=0;i<3;i++){
        if (inOnCircle) Time.push_back(T[i]);
        vector<P> cross = crossCircleLine(T[i], T[(i+1)%3], r);
        if(cross.size() == 1){
            Time.push_back(cross[0]);
            Arc.push_back(cross[0]);
            type.push_back(inOnCircle);
            inOnCircle = !inOnCircle;
        }else if(cross.size() == 2){
            Time.push_back(cross[0]);
            Arc.push_back(cross[0]);
            type.push_back(inOnCircle);
            Time.push_back(cross[1]);
            Arc.push_back(cross[1]);
            type.push_back(!inOnCircle);
        }
    }
/*
    cout << "Time: " << endl;
    for(int i=0;i<Time.size();i++){
        cout << Time[i].real() << " " << Time[i].imag() << endl;
    }
    cout << "Arc: " << endl;
    for(int i=0;i<Arc.size();i++){
        cout << Arc[i].real() << " " << Arc[i].imag() << endl;
    }
    cout << "type: " << endl;
    for(int i=0;i<type.size();i++){
        cout << type[i] << endl;
    }
*/
    double S = 0;
    S += area(Time);
    if(Arc.size() != 0){
        if(!type[0]){
            for(int i=0;i<(Arc.size()/2)-1;i++){
                S += arcArea(Arc[2*i+1], Arc[2*i+2],r);
            }
            S += arcArea(Arc[Arc.size()-1], Arc[0],r);
        }else{
            for(int i=0;i<Arc.size()/2;i++){
                S += arcArea(Arc[2*i], Arc[2*i+1], r);
            }
        }
    }
    return S;
}

double intersectionOfCircleAndPolygon(Poly &G, double r){
    vector<triangle> ears = triangulation(G);
    double S = 0;
    for (int i=0;i<ears.size();i++){
        S += intersectionCicleTriangle(ears[i], r);
    }
    return S;

}

int main(){
    int n;
    double r, x, y;
    cin >> n >> r;
    Poly G;
    for (int i=0;i<n;i++){
        P p;
        cin >> p.real() >> p.imag();
        G.push_back(p);
    }

    printf("%.12f\n", intersectionOfCircleAndPolygon(G, r));

    return 0;
}

Compiler output:
a.cc: In function 'double intersectionCicleTriangle(triangle&, double)':
a.cc:143:62: error: 'z' was not declared in this scope
  143 |     if(!crossing && norm(T[0]) > r*r + EPS && containment(T, z)) return M_PI*r*r;
      |                                                              ^
a.cc: In function 'int main()':
a.cc:215:13: error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'double')
  215 |         cin >> p.real() >> p.imag();
      |         ~~~ ^~ ~~~~~~~~
      |         |            |
      |         |            double
      |         std::istream {aka std::basic_istream<char>}
In file included from /usr/include/c++/14/iostream:42,
                 from a.cc:1:
/usr/include/c++/14/istream:170:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  170 |       operator>>(bool& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:170:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'bool&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:174:7: note: candidate: 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]' (near match)
  174 |       operator>>(short& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:174:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'short int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:177:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  177 |       operator>>(unsigned short& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:177:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'short unsigned int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:181:7: note: candidate: 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]' (near match)
  181 |       operator>>(int& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:181:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:184:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  184 |       operator>>(unsigned int& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:184:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'unsigned int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:188:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  188 |       operator>>(long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:188:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'long int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:192:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  192 |       operator>>(unsigned long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:192:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'long unsigned int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:199:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  199 |       operator>>(long long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:199:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'long long int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:203:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  203 |       operator>>(unsigned long long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:203:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'long long unsigned int&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:219:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  219 |       operator>>(float& __f)
      |       ^~~~~~~~
/usr/include/c++/14/istream:219:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'float&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:223:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  223 |       operator>>(double& __f)
      |       ^~~~~~~~
/usr/include/c++/14/istream:223:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'double&' to an rvalue of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:227:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  227 |       operator>>(long double& __f)
      |       ^~~~~~~~
/usr/include/c++/14/istream:227:7: note:   conversion of argument 1 would be ill-formed:
a.cc:215:22: error: cannot bind non-const lvalue reference of type 'long double&' to a value of type 'double'
  215 |         cin >> p.real() >> p.imag();
      |                ~~~~~~^~
/usr/include/c++/14/istream:328:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  328 |       operator>>(void*& __p)
      |       ^~~~~~~~
/usr/include/c++/14/istream:328:25: note:   no known conversion for argument 1 from 'double' to 'void*&'
  328 |       operator>>(void*& __p)
      |                  ~~~~~~~^~~
/usr/include/c++/14/istream:122:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(__istream_type& (*)(__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]'
  122 |       operator>>(__istream_type& (*__pf)(__istream_type&))
      |       ^~~~~~~~
/usr/include/c++/14/istream:122:36: note:   no known conversion for argument 1 from 'double' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&)' {aka 'std::basic_istream<char>& (*)(std::basic_istream<char>&)'}
  122 |       operator>>(__istream_type& (*__pf)(__istream_type&))
      |                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/istream:126:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(__ios_type& (*)(__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>; __ios_type = std::basic_ios<char>]'
  126 |       operator>>(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/usr/include/c++/14/istream:126:32: note:   no known conversion for argument 1 from 'double' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&)' {aka 'std::basic_ios<char>& (*)(std::basic_ios<char>&)'}
  126 |       operator>>(__ios_type& (*__pf)(__ios_type&))
      |                  ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/istream:133:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)