跳到正文

剪贴板

剪贴板 ix0nli9m

原文
剪贴板信息

剪贴板信息

收录
更新
C++
namespace hezlik{

  template<class T,class type_less_T=std::less<T>>
  class geometry_2d{

  public:
    using type=T;
    using type_less_type=type_less_T;

    using size_type=std::size_t;
    using difference_type=std::ptrdiff_t;
    using case_type=std::ptrdiff_t;

    static constexpr type_less_type type_less=type_less_type();

    static case_type sgn(const type &a,const type &b){
      return type_less(a,b)?-1:type_less(b,a)?1:0;
    }

    static case_type sgn(const type &a){return sgn(a,type());}

  protected:
    static type type_min(const type &x,const type &y){return type_less(x,y)?x:y;}
    static type type_max(const type &x,const type &y){return type_less(x,y)?y:x;}

    static type type_abs(const type &x){return abs(x);}
    static type type_sqrt(const type &x){return sqrt(x);}
    static type type_hypot(const type &x,const type &y){return std::hypot(x,y);}
    static type type_sin(const type &x){return sin(x);}
    static type type_cos(const type &x){return cos(x);}
    static type type_acos(const type &x){return acos(x);}
    static type type_atan2(const type &x,const type &y){return atan2(x,y);}

  public:
    class point;
    class line;
    class linear_function;
    class quadratic_function;
    class circle;

    template<class container_type>
    class polygon;

  private:
    using line_f=linear_function;
    using quad_f=quadratic_function;
    using root_list=std::pair<bool,std::vector<type>>;

  public:

    class point{

    private:
      using _pair=std::pair<type,type>;
      using _init_list=std::initializer_list<type>;

    public:
      type x,y;

      point()=default;

      point(const type &_x,const type &_y):x(_x),y(_y){}
      point(type &&_x,type &&_y):x(_x),y(_y){}

      point(const _pair &other):x(other.first),y(other.second){}
      point(_pair &&other):x(std::move(other.first)),y(std::move(other.second)){}

      point &operator = (const _pair &other){return *this=point(other);}
      point &operator = (_pair &&other){return *this=point(other);}

      point(const _init_list init):x(init.begin()[0]),y(init.begin()[1]){}

      point &operator = (const _init_list init){return *this=point(init);}

      explicit operator _pair ()const{return _pair(x,y);}
      _pair make_pair()const{return _pair(x,y);}
      friend _pair make_pair(const point &a){return a.make_pair();}

      type *data(){return &x;}  // Maybe dangerous
      const type *data()const{return &x;}  // Maybe dangerous

      void swap(point &other){std::swap(x,other.x);std::swap(y,other.y);}

      using reference=type &;
      using const_reference=const type &;

      reference operator [] (size_type p){return p?y:x;}
      const_reference operator [] (size_type p)const{return p?y:x;}

      explicit operator bool ()const{return sgn(x)||sgn(y);}
      bool operator ! ()const{return !sgn(x)&&!sgn(y);}

      friend bool operator == (const point &a,const point &b){
        return !sgn(a.x,b.x)&&!sgn(a.y,b.y);
      }

      friend bool operator != (const point &a,const point &b){
        return sgn(a.x,b.x)||sgn(a.y,b.y);
      }

      point &operator += (const point &p){x+=p.x;y+=p.y;return *this;}
      point &operator -= (const point &p){x-=p.x;y-=p.y;return *this;}
      point &operator *= (const type &p){x*=p;y*=p;return *this;}
      point &operator /= (const type &p){x/=p;y/=p;return *this;}
      point operator + ()const{return *this;}
      point operator - ()const{return point(-x,-y);}
      friend point operator + (const point &a,const point &b){return point(a)+=b;}
      friend point operator - (const point &a,const point &b){return point(a)-=b;}
      friend point operator * (const point &a,const type &b){return point(a)*=b;}
      friend point operator * (const type &a,const point &b){return point(b)*=a;}
      friend point operator / (const point &a,const type &b){return point(a)/=b;}

      type dot(const point &b)const{return x*b.x+y*b.y;}
      type cross(const point &b)const{return x*b.y-y*b.x;}

      friend type operator * (const point &a,const point &b){return a.dot(b);}
      friend type operator % (const point &a,const point &b){return a.cross(b);}

      type mod2()const{return x*x+y*y;}
      type mod()const{return type_hypot(x,y);}
      type dis2(const point &p)const{return (*this-p).mod2();}
      type dis(const point &p)const{return (*this-p).mod();}

      point unit()const{return *this/mod();}

      type ang()const{return type_atan2(y,x);}
      type ang(const point &p)const{return type_atan2(*this%p,*this*p);}

      point rot(const type &th)const{
        type s=type_sin(th),c=type_cos(th);
        return point(c*x-s*y,s*x+c*y);
      }

      point norm()const{return point(-y,x);}

    };

    static type dot(const point &a,const point &b){return a.dot(b);}
    static type cross(const point &a,const point &b){return a.cross(b);}

    static type mod2(const point &a){return a.mod2();}
    static type mod(const point &a){return a.mod();}
    static type dis2(const point &a,const point &b){return a.dis2(b);}
    static type dis(const point &a,const point &b){return a.dis(b);}

    static point unit(const point &a){return a.unit();}

    static type ang(const point &a){return a.ang();}
    static type ang(const point &a,const point &b){return a.ang(b);}

    static point rot(const point &a,const type &b){return a.rot(b);}
    static point norm(const point &a){return a.norm();}

    class line{

    private:
      using _pair=std::pair<point,point>;
      using _init_list=std::initializer_list<point>;

    public:
      point s,t;

      line()=default;

      line(const point &_s,const point &_t):s(_s),t(_t){}
      line(point &&_s,point &&_t):s(_s),t(_t){}

      line(const _pair &other):s(other.first),t(other.second){}
      line(_pair &&other):s(std::move(other.first)),t(std::move(other.second)){}

      line &operator = (const _pair &other){return *this=line(other);}
      line &operator = (_pair &&other){return *this=line(other);}

      line(const _init_list init):s(init.begin()[0]),t(init.begin()[1]){}

      line &operator = (const _init_list init){return *this=line(init);}

      explicit line(const line_f &other):s(type(),other(type())),t(type(1),other(type(1))){}

      line &operator = (const line_f &other){return *this=line(other);}

      explicit operator _pair ()const{return _pair(s,t);}
      _pair make_pair()const{return _pair(s,t);}
      friend _pair make_pair(const line &a){return a.make_pair();}

      point *data(){return &s;}  // Maybe dangerous
      const point *data()const{return &s;}  // Maybe dangerous

      void swap(line &other){std::swap(s,other.s);std::swap(t,other.t);}

      using reference=point &;
      using const_reference=const point &;

      reference operator [] (size_type p){return p?t:s;}
      const_reference operator [] (size_type p)const{return p?t:s;}

      point vec()const{return t-s;}

      type operator () (const type &x)const{return (s+(x-s.x)/(t.x-s.x)*(t-s)).y;}

      bool on(const point &k)const{return !sgn(vec()%(k-s));}

      bool segment_on(const point &k)const{
        return on(k)&&!type_less(k.x,type_min(s.x,t.x))
                    &&!type_less(type_max(s.x,t.x),k.x)
                    &&!type_less(k.y,type_min(s.y,t.y))
                    &&!type_less(type_max(s.y,t.y),k.y);
      }

      point pro(const point &k)const{return s+(k-s)*vec()*vec()/vec().mod2();}
      point ref(const point &k)const{return type(2)*pro(k)-k;}
      type dis2(const point &k)const{return k.dis2(pro(k));}
      type dis(const point &k)const{return k.dis(pro(k));}

      point _cross(const line &b)const{
        const line &a=*this;
        point u=a.s-b.s,v=a.vec(),w=b.vec();
        return a.s+(w%u)/(v%w)*v;
      }

      case_type para(const line &b)const{
        const line &a=*this;
        return sgn(a.vec()%b.vec())?0:sgn((a.s-b.s)%a.vec())?1:-1;
      }

      friend bool operator == (const line &a,const line &b){return a.para(b)==-1;}
      friend bool operator != (const line &a,const line &b){return a.para(b)!=-1;}

      std::pair<case_type,point> cross(const line &b)const{
        case_type o=para(b);
        return std::pair<case_type,point>(o,o?point():_cross(b));
      }

      bool check_segment_cross(const line &b)const{
        const line &a=*this;
        case_type o=a.para(b);
        if (o==1) return 0;
        if (o==-1){
          if (type_less(type_max(a.s.x,a.t.x),type_min(b.s.x,b.t.x))) return 0;
          if (type_less(type_max(b.s.x,b.t.x),type_min(a.s.x,a.t.x))) return 0;
          if (type_less(type_max(a.s.y,a.t.y),type_min(b.s.y,b.t.y))) return 0;
          if (type_less(type_max(b.s.y,b.t.y),type_min(a.s.y,a.t.y))) return 0;
          return 1;
        }
        if (sgn((a.s-b.s)%b.vec())*sgn((a.t-b.s)%b.vec())>0) return 0;
        if (sgn((b.s-a.s)%a.vec())*sgn((b.t-a.s)%a.vec())>0) return 0;
        return 1;
      }

      std::vector<point> segment_cross(const line &b)const{
        const line &a=*this;
        case_type o=a.para(b);
        if (o==1) return {};
        if (o==-1){
          if (type_less(type_max(a.s.x,a.t.x),type_min(b.s.x,b.t.x))) return {};
          if (type_less(type_max(b.s.x,b.t.x),type_min(a.s.x,a.t.x))) return {};
          if (type_less(type_max(a.s.y,a.t.y),type_min(b.s.y,b.t.y))) return {};
          if (type_less(type_max(b.s.y,b.t.y),type_min(a.s.y,a.t.y))) return {};
          auto max=[&](const point &a,const point &b)->point{
            case_type o=sgn(a.x,b.x);
            return o?o==1?a:b:type_less(a.y,b.y)?b:a;
          };
          auto min=[&](const point &a,const point &b)->point{
            case_type o=sgn(a.x,b.x);
            return o?o==1?b:a:type_less(a.y,b.y)?a:b;
          };
          return {
            max(min(a.s,a.t),min(b.s,b.t)),
            min(max(a.s,a.t),max(b.s,b.t))
          };
        }
        if (sgn((a.s-b.s)%b.vec())*sgn((a.t-b.s)%b.vec())>0) return {};
        if (sgn((b.s-a.s)%a.vec())*sgn((b.t-a.s)%a.vec())>0) return {};
        return {a._cross(b)};
      }

      type segment_dis2(const point &k)const{
        point u=k-s,v=k-t,w=vec();
        return k.dis2(type_less(u*w,type())?s:type_less(type(),v*w)?t:pro(k));
      }

      type segment_dis(const point &k)const{
        point u=k-s,v=k-t,w=vec();
        return k.dis(type_less(u*w,type())?s:type_less(type(),v*w)?t:pro(k));
      }

      type segment_dis2(const line &b)const{
        const line &a=*this;
        if (a.check_segment_cross(b)) return type();
        return type_min(type_min(a.segment_dis2(b.s),a.segment_dis2(b.t)),
                        type_min(b.segment_dis2(a.s),b.segment_dis2(a.t)));
      }

      type segment_dis(const line &b)const{
        const line &a=*this;
        if (a.check_segment_cross(b)) return type();
        return type_min(type_min(a.segment_dis(b.s),a.segment_dis(b.t)),
                        type_min(b.segment_dis(a.s),b.segment_dis(a.t)));
      }

    };

    static point vec(const line &a){return a.vec();}

    static type value(const line &a,const type &x){return a(x);}

    static bool on(const line &a,const point &k){return a.on(k);}
    static bool segment_on(const line &a,const point &k){return a.segment_on(k);}

    static point pro(const line &a,const point &k){return a.pro(k);}
    static point ref(const line &a,const point &k){return a.ref(k);}
    static type dis2(const line &a,const point &k){return a.dis2(k);}
    static type dis(const line &a,const point &k){return a.dis(k);}

    static point _cross(const line &a,const line &b){return a._cross(b);}
    static case_type para(const line &a,const line &b){return a.para(b);}
    static std::pair<case_type,point> cross(const line &a,const line &b){return a.cross(b);}

    static bool check_segment_cross(const line &a,const line &b){
      return a.check_segment_cross(b);
    }

    static std::vector<point> segment_cross(const line &a,const line &b){
      return a.segment_cross(b);
    }

    static type segment_dis2(const line &a,const point &k){return a.segment_dis2(k);}
    static type segment_dis(const line &a,const point &k){return a.segment_dis(k);}
    static type segment_dis2(const line &a,const line &b){return a.segment_dis2(b);}
    static type segment_dis(const line &a,const line &b){return a.segment_dis(b);}

    class linear_function{

    private:
      using _pair=std::pair<type,type>;
      using _init_list=std::initializer_list<type>;

    public:
      type b,k;

      linear_function()=default;

      linear_function(const type &_k,const type &_b):k(_k),b(_b){};
      linear_function(type &&_k,type &&_b):k(_k),b(_b){};

      linear_function(const _pair &other):k(other.first),b(other.second){}
      linear_function(_pair &&other):k(std::move(other.first)),b(std::move(other.second)){}

      line_f &operator = (const _pair &other){return *this=line_f(other);}
      line_f &operator = (_pair &&other){return *this=line_f(other);}

      linear_function(const _init_list init):k(init.begin()[0]),b(init.begin()[1]){}

      line_f &operator = (const _init_list init){return *this=line_f(init);}

      explicit linear_function(const line &other)
        :k((other.t.y-other.s.y)/(other.t.x-other.s.x))
        ,b(other.s.y-k*other.s.x){}

      line_f &operator = (const line &other){return *this=line_f(other);}

      explicit operator _pair ()const{return _pair(k,b);}
      _pair make_pair()const{return _pair(k,b);}
      friend _pair make_pair(const line_f &a){return a.make_pair();}

      type *data(){return &b;}  // Maybe dangerous
      const type *data()const{return &b;}  // Maybe dangerous

      void swap(line_f &other){std::swap(k,other.k);std::swap(b,other.b);}

      using reference=type &;
      using const_reference=const type &;

      reference operator [] (size_type p){return p?b:k;}
      const_reference operator [] (size_type p)const{return p?b:k;}

      type operator () (const type &x)const{return k*x+b;}

      friend bool operator == (const line_f &a,const line_f &b){
        return !sgn(a.k,b.k)&&!sgn(a.b,b.b);
      }

      friend bool operator != (const line_f &a,const line_f &b){
        return sgn(a.k,b.k)||sgn(a.b,b.b);
      }

      line_f &operator += (const line_f &p){k+=p.k;b+=p.b;return *this;}
      line_f &operator -= (const line_f &p){k-=p.k;b-=p.b;return *this;}
      line_f operator + ()const{return *this;}
      line_f operator - ()const{return line_f(-k,-b);}
      friend line_f operator + (const line_f &a,const line_f &b){return line_f(a)+=b;}
      friend line_f operator - (const line_f &a,const line_f &b){return line_f(a)-=b;}

      line_f &operator *= (const type &p){k*=p;b*=p;return *this;}
      line_f &operator /= (const type &p){k/=p;b/=p;return *this;}
      friend line_f operator * (const line_f &a,const type &b){return line_f(a)*=b;}
      friend line_f operator * (const type &a,const line_f &b){return line_f(b)*=a;}
      friend line_f operator / (const line_f &a,const type &b){return line_f(a)/=b;}

      type min(const type &l,const type &r)const{return k*(type_less(type(),k)?l:r)+b;}
      type max(const type &l,const type &r)const{return k*(type_less(k,type())?l:r)+b;}

      type slope(const type &x)const{return k;}
      type area(const type &l,const type &r)const{return (k*(l+r)/type(2)+b)*(r-l);}

      type _root()const{return -b/k;}
      root_list root()const{return sgn(k)?root_list(1,{_root()}):root_list(!!sgn(b),{});}

    };

    static type value(const line_f &a,const type &x){return a(x);}

    static type min(const line_f &a,const type &l,const type &r){return a.min(l,r);}
    static type max(const line_f &a,const type &l,const type &r){return a.max(l,r);}

    static type slope(const line_f &a,const type &x){return a.slope(x);}
    static type area(const line_f &a,const type &l,const type &r){return a.area(l,r);}

    static type _root(const line_f &a){return a._root();}
    static root_list root(const line_f &a){return a.root();}

    template<class container_T=std::vector<point>>
    class polygon
    :public container_T{

    private:
      using _init_list=std::initializer_list<point>;

    public:
      using container_type=container_T;

      using container_type::container_type;
      using container_type::size;
      using container_type::empty;
      using container_type::back;
      using container_type::push_back;
      using container_type::pop_back;

      polygon(const container_type &other):container_type(other){}
      polygon(container_type &&other):container_type(other){}

      polygon &operator = (const container_type &other){return *this=polygon(other);}
      polygon &operator = (container_type &&other){return *this=polygon(other);}

      polygon &operator = (const _init_list init){return *this=polygon(init);}

      explicit operator container_type ()const{return *this;}
      container_type &container(){return *this;}
      const container_type &container()const{return *this;}

      void swap(polygon &other){container_type::swap(other);}

      friend bool operator == (const polygon &a,const polygon &b){
        if (a.size()!=b.size()) return 0;
        size_type p=0;
        for (;p<a.size()&&a[p]!=b[0];++p);
        for (size_type i=0;i<a.size();++i)
          if (a[p>=a.size()-i?p-(a.size()-i):p+i]!=b[i]) return 0;
        return 1;
      }

      friend bool operator != (const polygon &a,const polygon &b){return !(a==b);}

    protected:
      size_type pre(size_type p)const{return p?p-1:size()-1;}
      size_type suf(size_type p)const{return p+1==size()?0:p+1;}

    public:
      type girth2()const{
        const polygon &a=*this;
        type res=type();
        for (size_type i=0;i<a.size();++i)
          res+=dis2(a[i],a[suf(i)]);
        return res;
      }

      type len2()const{return girth2();}

      type girth()const{
        const polygon &a=*this;
        type res=type();
        for (size_type i=0;i<a.size();++i)
          res+=dis(a[i],a[suf(i)]);
        return res;
      }

      type len()const{return girth();}

      type area2()const{
        const polygon &a=*this;
        type res=type();
        for (size_type i=0;i<a.size();++i)
          res+=a[i]%a[suf(i)];
        return type_abs(res);
      }

      type area()const{return area2()/type(2);}

      bool on(const point &k)const{
        const polygon &a=*this;
        for (size_type i=0;i<a.size();++i){
          point u=a[i]-k,v=a[suf(i)]-k;
          if (!sgn(u%v)&&!type_less(type(),u*v)) return 1;
        }
        return 0;
      }

      bool in(const point &k)const{
        const polygon &a=*this;
        type res=type();
        for (size_type i=0;i<a.size();++i){
          point u=a[i]-k,v=a[suf(i)]-k;
          if (!sgn(u%v)&&!type_less(type(),u*v)) return 1;
          res+=u.angle(v);
        }
        return sgn(res);
      }

      polygon _convex_hull()const{
        if (size()<3) return *this;
        const polygon &a=*this;
        polygon res;
        for (size_type i=0;i<a.size();++i){
          for (;res.size()>=2;res.pop_back())
            if (type_less(type(),(res.back()-res.end()[-2])%(a[i]-res.back()))) break;
          res.push_back(a[i]);
        }
        size_type lim=res.size();
        for (size_type i=a.size()-2;;--i){
          for (;res.size()>lim;res.pop_back())
            if (type_less(type(),(res.back()-res.end()[-2])%(a[i]-res.back()))) break;
          res.push_back(a[i]);
          if (!i) break;
        }
        res.pop_back();
        return res;
      }

      polygon convex_hull()const{
        polygon a=*this;
        sort(a.begin(),a.end(),[&](const point &a,const point &b)->bool{
          case_type o=sgn(a.y,b.y);
          return o?o<0:type_less(a.x,b.x);
        });
        return a._convex_hull();
      }

    };

    #define _temp template<class container_type>
    #define _polygon polygon<container_type>

    _temp static type girth2(const _polygon &a){return a.girth2();}
    _temp static type len2(const _polygon &a){return a.girth2();}
    _temp static type girth(const _polygon &a){return a.girth();}
    _temp static type len(const _polygon &a){return a.girth();}
    _temp static type area2(const _polygon &a){return a.area2();}
    _temp static type area(const _polygon &a){return a.area();}
    _temp static bool on(const _polygon &a,const point &k){return a.on(k);}
    _temp static bool in(const _polygon &a,const point &k){return a.in(k);}
    _temp static _polygon _convex_hull(const _polygon &a){return a._convex_hull();}
    _temp static _polygon convex_hull(const _polygon &a){return a.convex_hull();}

    #undef _temp
    #undef _polygon

    class quadratic_function{

    private:
      using _tuple=std::tuple<type,type,type>;
      using _init_list=std::initializer_list<type>;

    public:
      type c,b,a;

      quadratic_function()=default;

      quadratic_function(const type &_a,const type &_b,const type &_c):a(_a),b(_b),c(_c){};
      quadratic_function(type &&_a,type &&_b,type &&_c):a(_a),b(_b),c(_c){};

      quadratic_function(const _tuple &other)
        :a(std::get<0>(other))
        ,b(std::get<1>(other))
        ,c(std::get<2>(other)){}

      quadratic_function(_tuple &&other)
        :a(std::move(std::get<0>(other)))
        ,b(std::move(std::get<1>(other)))
        ,c(std::move(std::get<2>(other))){}

      quad_f &operator = (const _tuple &other){return *this=quad_f(other);}
      quad_f &operator = (_tuple &&other){return *this=quad_f(other);}

      quadratic_function(const _init_list init)
        :a(init.begin()[0]),b(init.begin()[1]),c(init.begin()[2]){}

      quad_f &operator = (const _init_list init){return *this=quad_f(init);}

      explicit operator _tuple ()const{return _tuple(a,b,c);}
      _tuple make_tuple()const{return _tuple(a,b,c);}
      friend _tuple make_tuple(const quad_f &other){return other.make_tuple();}

      type *data(){return &c;}  // Maybe dangerous
      const type *data()const{return &c;}  // Maybe dangerous

      void swap(quad_f &other){std::swap(a,other.a);std::swap(b,other.b);std::swap(c,other.c);}

      using reference=type &;
      using const_reference=const type &;

      reference operator [] (size_type p){return p?p==2?a:b:c;}
      const_reference operator [] (size_type p)const{return p?p==2?a:b:c;}

      type operator () (const type &x)const{return (a*x+b)*x+c;}

      friend bool operator == (const quad_f &a,const quad_f &b){
        return !sgn(a.a,b.a)&&!sgn(a.b,b.b)&&!sgn(a.c,b.c);
      }

      friend bool operator != (const quad_f &a,const quad_f &b){
        return sgn(a.a,b.a)||sgn(a.b,b.b)||sgn(a.c,b.c);
      }

      quad_f &operator += (const quad_f &p){a+=p.a;b+=p.b;c+=p.c;return *this;}
      quad_f &operator -= (const quad_f &p){a-=p.a;b-=p.b;c-=p.c;return *this;}
      quad_f operator + ()const{return *this;}
      quad_f operator - ()const{return quad_f(-a,-b,-c);}
      friend quad_f operator + (const quad_f &a,const quad_f &b){return quad_f(a)+=b;}
      friend quad_f operator - (const quad_f &a,const quad_f &b){return quad_f(a)-=b;}

      quad_f &operator *= (const type &p){a*=p;b*=p;c*=p;return *this;}
      quad_f &operator /= (const type &p){a/=p;b/=p;c/=p;return *this;}
      friend quad_f operator * (const quad_f &a,const type &b){return quad_f(a)*=b;}
      friend quad_f operator * (const type &a,const quad_f &b){return quad_f(b)*=a;}
      friend quad_f operator / (const quad_f &a,const type &b){return quad_f(a)/=b;}

      type mid()const{return -b/(type(2)*a);}

      type min(const type &l,const type &r)const{
        case_type o=sgn(a);
        if (!o) return line_f(b,c).min(l,r);
        if (o<0) return type_min((*this)(l),(*this)(r));
        type x=mid();
        return (*this)(type_less(x,l)?l:type_less(r,x)?r:x);
      }

      type max(const type &l,const type &r)const{
        case_type o=sgn(a);
        if (!o) return line_f(b,c).max(l,r);
        if (o>0) return type_max((*this)(l),(*this)(r));
        type x=mid();
        return (*this)(type_less(x,l)?l:type_less(r,x)?r:x);
      }

      type slope(const type &x)const{return type(2)*a*x+b;}

      type area(const type &l,const type &r)const{
        quad_f t(a/type(3),b/type(2),c);
        return r*t(r)-l*t(l);
      }

      std::vector<type> _root()const{
        type delta=b*b-type(4)*a*c;
        switch (sgn(delta)){
          case -1:return {};
          case 0:return {-b/(type(2)*a)};
          case 1:{
            delta=type_sqrt(delta);
            type t=type(2)*a;
            return {(-b-delta)/t,(-b+delta)/t};
          }
        }
        return {};
      }

      root_list root()const{
        if (!sgn(a)) return line_f(b,c).root();
        return root_list(1,_root());
      }

    };

    static type value(const quad_f &a,const type &x){return a(x);}

    static type mid(const quad_f &a){return a.mid();}
    static type min(const quad_f &a,const type &l,const type &r){return a.min(l,r);}
    static type max(const quad_f &a,const type &l,const type &r){return a.max(l,r);}

    static type slope(const quad_f &a,const type &x){return a.slope(x);}
    static type area(const quad_f &a,const type &l,const type &r){return a.area(l,r);}

    static type _root(const quad_f &a){return a._root();}
    static root_list root(const quad_f &a){return a.root();}

    static constexpr type pi=3.14159265358979323851;

    class circle{

    private:
      using _pair=std::pair<point,type>;
      using _tuple=std::tuple<type,type,type>;
      using _init_list=std::initializer_list<type>;
      using _tuple_p=std::tuple<point,point,point>;
      using _init_list_p=std::initializer_list<type>;

    public:
      point o;
      type r;

      circle()=default;

      circle(const point &_o,const type &_r):o(_o),r(_r){}
      circle(point &&_o,type &&_r):o(_o),r(_r){}

      circle(const _pair &other):o(other.first),r(other.second){}
      circle(_pair &&other):o(std::move(other.first)),r(std::move(other.second)){}

      circle &operator = (const _pair &other){return *this=point(other);}
      circle &operator = (_pair &&other){return *this=point(other);}

      circle(const type &x,const type &y,const type &_r):o(point(x,y)),r(_r){}
      circle(type &&x,type &&y,type &&_r):o(point(x,y)),r(_r){}

      circle(const _tuple &other)
        :o(point(std::get<0>(other),std::get<1>(other))
        ,std::get<2>(other)){}

      circle(_tuple &&other)
        :o(point(std::move(std::get<0>(other)),std::move(std::get<1>(other)))
        ,std::move(std::get<2>(other))){}

      circle &operator = (const _tuple &other){return *this=point(other);}
      circle &operator = (_tuple &&other){return *this=point(other);}

      circle(const _init_list init)
        :o(point(init.begin()[0],init.begin()[1])),r(init.begin()[2]){}

      circle &operator = (const _init_list init){return *this=quad_f(init);}

    protected:
      void _circle(point a,point b,point c){
        if (sgn((a-b)%(b-c))){
          o=(a+b)/type(2);
          line p(o,o+(a-b).norm());
          o=(b+c)/type(2);
          line q(o,o+(b-c).norm());
          o=p._cross(q);
          r=o.dis(a);
        }else{
          auto cmp=[&](const point &a,const point &b)->bool{
            case_type o=sgn(a.x,b.x);
            return o?o<0:type_less(a.y,b.y);
          };
          if (cmp(a,b)) a.swap(b);
          if (cmp(a,c)) a.swap(c);
          if (cmp(b,c)) b.swap(c);
          o=(a+c)/type(2);
          r=o.dis(a);
        }
      }

    public:
      circle(const point &a,const point &b,const point &c){_circle(a,b,c);}

      circle(const _tuple_p &other){
        _circle(std::get<0>(other),std::get<1>(other),std::get<2>(other));
      }

      circle &operator = (const _tuple_p &other){return *this=circle(other);}

      circle(const _init_list_p &other){
        _circle(other.begin()[0],other.begin()[1],other.begin()[2]);
      }

      circle &operator = (const _init_list_p &other){return *this=circle(other);}

      explicit operator _pair ()const{return _pair(o,r);}
      _pair make_pair()const{return _pair(o,r);}
      friend _pair make_pair(const circle &other){return other.make_pair();}

      explicit operator _tuple ()const{return _tuple(o.x,o.y,r);}
      _tuple make_tuple()const{return _tuple(o.x,o.y,r);}
      friend _tuple make_tuple(const circle &other){return other.make_tuple();}

      void swap(circle &other){o.swap(other.o);std::swap(r,other.r);}

      friend bool operator == (const circle &a,const circle &b){
        return a.o==b.o&&!sgn(a.r,b.r);
      }

      friend bool operator != (const circle &a,const circle &b){
        return a.o!=b.o||!sgn(a.r,b.r);
      }

      point operator () (const type &th)const{return o+point(type_cos(th),type_sin(th))*r;}

      bool on(const point &k)const{return !sgn(o.dis2(k),r*r);}
      bool in(const point &k)const{return !type_less(r*r,o.dis2(k));}

      type rad()const{return r;}
      type dia()const{return type(2)*r;}
      type girth()const{return type(2)*pi*r;}
      type len()const{return type(2)*pi*r;}
      type area()const{return pi*r*r;}

      std::vector<point> cross(const line &b)const{
        const circle &a=*this;
        point k=b.pro(a.o);
        type d2=a.o.dis2(k),r2=a.r*a.r;
        switch (sgn(d2,r2)){
          case 1:return {};
          case 0:return {k};
          case -1:{
            point v=b.vec()*type_sqrt((r2-d2)/b.vec().mod2());
            return {k+v,k-v};
          }
        }
        return {};
      }

      std::vector<point> cross(const circle &b)const{
        const circle &a=*this;
        auto sqr=[&](const type &x)->type{return x*x;};
        type d2=a.o.dis2(b.o);
        if (type_less(d2,sqr(a.r-b.r))) return {};
        switch (sgn(d2,sqr(a.r+b.r))){
          case 1:return {};
          case 0:return {a.o+(b.o-a.o)*a.r/(a.r+b.r)};
          case -1:{
            std::vector<point> res;
            type u=a.o.x-b.o.x,v=a.o.y-b.o.y;
            type w=b.r*b.r-a.r*a.r-u*u-v*v;
            u*=type(2)*a.r;v*=type(2)*a.r;
            if (sgn(v)){
              std::vector<type> d=quad_f(u*u+v*v,-type(2)*u*w,w*w-v*v).root().second;
              for (const type &c:d){
                type s=(w-u*c)/v;
                res.push_back(a.o+a.r*point(c,s));
              }
            }else{
              std::vector<type> d=quad_f(u*u+v*v,-type(2)*v*w,w*w-u*u).root().second;
              for (const type &s:d){
                type c=(w-v*s)/u;
                res.push_back(a.o+a.r*point(c,s));
              }
            }
            return res;
          }
        }
        return {};
      }

      std::vector<line> tan(const point &b)const{
        const circle &a=*this;
        type d2=b.dis2(a.o),r2=a.r*a.r;
        switch (sgn(d2,r2)){
          case -1:return {};
          case 0:return {line(b,b+(a.o-b).norm())};
          case 1:{
            std::vector<point> t=a.cross(circle(b,type_sqrt(d2-r2)));
            return {line(t[0],b),line(t[1],b)};
          }
        }
        return {};
      }

      std::vector<line> intan(const circle &b)const{
        const circle &a=*this;
        std::vector<line> res=circle(a.o,a.r+b.r).tan(b.o);
        for (line &t:res){
          point v=(a.o-t[0])*b.r/(a.r+b.r);
          t=line(t[0]+v,t[1]+v);
        }
        return res;
      }

      std::vector<line> extan(circle b)const{
        circle a=*this;
        case_type o=sgn(a.r,b.r);
        if (!o){
          point v=(b.o-a.o).norm().unit()*a.r;
          return {line(a.o+v,b.o+v),line(a.o-v,b.o-v)};
        }
        bool flag=0;
        if (o<0) a.swap(b),flag=1;
        std::vector<line> res=circle(a.o,a.r-b.r).tan(b.o);
        for (line &t:res){
          point v=(t[0]-a.o)*b.r/(a.r-b.r);
          t=flag&&res.size()>1?line(t[1]+v,t[0]+v):line(t[0]+v,t[1]+v);
        }
        return res;
      }

      std::vector<line> tan(const circle &b)const{
        std::vector<line> res=extan(b),tmp=intan(b);
        for (const line &t:tmp) res.push_back(t);
        return res;
      }

    };

    static point value(const circle &a,const type &th){return a(th);}

    static bool on(const circle &a,const point &k){return a.on(k);}
    static bool in(const circle &a,const point &k){return a.in(k);}

    static type rad(const circle &a){return a.rad();}
    static type dia(const circle &a){return a.dia();}
    static type girth(const circle &a){return a.girth();}
    static type len(const circle &a){return a.len();}
    static type area(const circle &a){return a.area();}

    static std::vector<point> cross(const circle &a,const line &b){return a.cross(b);}
    static std::vector<point> cross(const circle &a,const circle &b){return a.cross(b);}

    static std::vector<line> tan(const circle &a,const point &b){return a.tan(b);}
    static std::vector<line> intan(const circle &a,const circle &b){return a.intan(b);}
    static std::vector<line> extan(const circle &a,const circle &b){return a.extan(b);}
    static std::vector<line> tan(const circle &a,const circle &b){return a.tan(b);}

  };

}

namespace std{

  #define _temp template<class type,class less>
  #define _geo typename hezlik::geometry_2d<type,less>

  _temp void swap(_geo::point &a,_geo::point &b){a.swap(b);}
  _temp void swap(_geo::line &a,_geo::line &b){a.swap(b);}
  _temp void swap(_geo::line_f &a,_geo::line_f &b){a.swap(b);}
  _temp void swap(_geo::polygon &a,_geo::polygon &b){a.swap(b);}
  _temp void swap(_geo::quad_f &a,_geo::quad_f &b){a.swap(b);}
  _temp void swap(_geo::circle &a,_geo::circle &b){a.swap(b);}

  #undef _temp
  #undef _geo

}