模板

#include<bits/stdc++.h>

using namespace std;
#define ll long long

//https://blog.csdn.net/clasky/article/details/9990235
#define Point Vector
double eps=1e-8;

struct Vector;
struct Segment;

//向量类
struct Vector{
    double x,y;

    Vector(double xx=0.0,double yy=0.0){
        x=xx;
        y=yy;
    }

    //向量加减
    Vector operator+(Vector v){
        return Vector(x+v.x,y+v.y);
    }

    Vector operator-(Vector v){
        return Vector(x-v.x,y-v.y);
    }

    //向量缩放
    Vector operator*(double d){
        return Vector(x*d,y*d);
    }

    Vector operator/(double d){
        return Vector(x/d,y/d);
    }

    //向量点积、叉积
    double dot(Vector v){
        return x*v.x+y*v.y;
    }

    double cross(Vector v){
        return x*v.y-y*v.x;
    }

    //向量相等、不等
    bool operator==(Vector v){
        return (fabs(x-v.x)<eps)&&(fabs(y-v.y)<eps);
    }

    bool operator!=(Vector v){
        return (fabs(x-v.x)>=eps)||(fabs(y-v.y)>=eps);
    }

    //向量平行
    bool parallel(Vector v){
        return fabs(this->cross(v)<eps);
    }

    //向量长度
    double length(){
        return sqrt(x*x+y*y);
    }

    double length2(){
        return x*x+y*y;
    }

    //两点距离
    double distance(Vector v){
        return (v-*this).length();
    }

    double distance2(Vector v){
        return (v-*this).length2();
    }

    //在线段上,不包括线段的端点,要另外判断与线段端点重合的情形
    bool onSegment(Segment s){
        Vector v1=s.p1-*this;
        Vector v2=s.p2-*this;
        return fabs(v1.cross(v2))<eps&&v1.dot(v2)<0;
    }

    //在线段上,包括线段的端点
    bool onSegment2(Segment s){
        Vector v1=s.p1-*this;
        Vector v2=s.p2-*this;
        if(v1.length2()<eps||v2.length2()<eps)
            return true;
        return fabs(v1.cross(v2))<eps&&v1.dot(v2)<0;
    }

    //返回点到线段的最短距离点
    Point

    //返回点到线段所在直线的最短距离点

    //绕p点旋转a弧度

    //返回关于直线的对称点

    //返回两点的平分线

};

//线段类,用无穷远点代表射线、直线
struct Segment{
    Point p1,p2;
    Segment (Point pp1,Point pp2){
        p1=pp1;
        p2=pp2;
    }

    //判断是否平行和重合
    bool parallel(){

    }

    //判断与另一线段是否相交,相交则把交点存入
    bool insector(){

    }

    //判断与另一线段是否垂直,垂直则把垂足存入

    //判断两点是否在线段同侧

    //判断两点是否在线段异侧

    //返回一射线关于直线的反射线,并将反射点存储

};

struct Line{

};

struct Circle{

};

struct Triangle{

};


int main(){
    ;
}

 还是抄别人的好:

//By Candy?
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e4,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

const double eps=1e-8;
const double pi=acos(-1);

inline int sgn(double x){
    if(abs(x)<eps) return 0;
    else return x<0?-1:1;
}

struct Vector{
    double x,y;
    Vector(double a=0,double b=0):x(a),y(b){}
    bool operator <(const Vector &a)const{
        return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0);
    }
    void print(){printf("%lf %lf
",x,y);}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}

double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}

double Len(Vector a){return sqrt(Dot(a,a));}
double Len2(Vector a){return Dot(a,a);}
double Angle(Vector a,Vector b){
    return acos(Dot(a,b)/Len(a)/Len(b));
}
Vector Normal(Vector a){
    return Vector(-a.y,a.x);//counterClockwise
}
Vector Rotate(Vector a,double rad){
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}

//Line:use p and v
//struct Line{
//    Point p;
//    Vector v;
//    Line(){}
//    Line(Point p,Vector v):p(p),v(v){}
//    bool operator <(const Line a)const{
//        return sgn(Cross(v,a.v))>=0;
//    }
//};
//bool OnLeft(Line l,Point p){
//    return sgn(Cross(l.v,p-l.p))>=0;
//}
//Point LI(Line a,Line b){
//    Vector v=a.p-b.p;
//    double t=Cross(b.v,v)/Cross(a.v,b.v);
//    return a.p+a.v*t;
//}

//Line:use s and t
struct Line{
    Point s,t;
    Line(){}
    Line(Point a,Point b):s(a),t(b){}
    bool operator <(Line a)const{
        return sgn(Cross(t-s,a.t-a.s))>=0;
    }
};

double DisTL(Point p,Point a,Point b){
    Vector v1=b-a,v2=p-a;
    return abs(Cross(v1,v2)/Len(v1));
}
double DisTS(Point p,Point a,Point b){
    if(a==b) return Len(p-a);
    Vector v1=b-a,v2=p-a,v3=p-b;
    if(sgn(Dot(v1,v2))<0) return Len(v2);
    else if(sgn(Dot(v1,v3))>0) return Len(v3);
    else return abs(Cross(v1,v2)/Len(v1));
}
bool OnLeft(Line l,Point p){
    return sgn(Cross(l.t-l.s,p-l.s))>=0;
}
bool OnSeg(Point p,Point a,Point b){
    return DisTL(p,a,b)==0&&sgn(Dot(p-a,p-b)<0&&!(p==a))&&!(p==b);
}

Point LI(Line a,Line b){
    Vector v=a.s-b.s,v1=a.t-a.s,v2=b.t-b.s;
    double t=Cross(v2,v)/Cross(v1,v2);
    return a.s+v1*t;
}
bool isLSI(Line l1,Line l2){
    Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
    return sgn(Cross(v,u))!=sgn(Cross(v,w));
}
bool isSSI(Line l1,Line l2){
    return isLSI(l1,l2)&&isLSI(l1,l2);
}
//---chong he
//bool isSSI(Line l1,Line l2){
//    Vector v1=l1.t-l1.s,v2=l2.t-l2.s;
//    if(sgn(Cross(v1,v2))==0){
//        int flag=0;
//        Vector u=l2.s-l1.s,w=l2.t-l1.s;
//        if(sgn(Dot(u,w))<0) flag=1;
//        u=l2.s-l1.t,w=l2.t-l1.t;
//        if(sgn(Dot(u,w))<0) flag=1;
//        return flag;
//    }
//    else return isLSI(l1,l2)&&isLSI(l2,l1);
//}

Point Circumcenter(Point a,Point b,Point c){
    Point p=(a+b)/2,q=(a+c)/2;
    Vector v=Normal(b-a),u=Normal(c-a);
    if(sgn(Cross(v,u))==0){
        if(sgn(Len(a-b)+Len(b-c)-Len(a-c))==0) return (a+c)/2;
        if(sgn(Len(a-b)+Len(a-c)-Len(b-c))==0) return (b+c)/2;
        if(sgn(Len(a-c)+Len(b-c)-Len(a-b))==0) return (a+b)/2;
    }
    return LI(Line(p,p+v),Line(q,q+u));
}

Point Barycenter(Point a,Point b,Point c){
    return (a+b+c)/3;
}

bool cmpPolar(Point a,Point b){
    return sgn(Cross(a,b))>0;
}

int PointInPolygon(Point p,Point poly[],int n){
    int wn=0;
    for(int i=1;i<=n;i++){
        if(sgn(DisTS(p,poly[i],poly[i%n+1]))==0) return -1;
        int k=sgn(Cross(poly[i%n+1]-poly[i],p-poly[i])),
        d1=sgn(poly[i].y-p.y),d2=sgn(poly[i%n+1].y-p.y);
        if(k>0&&d1<=0&&d2>0) wn++;
        if(k<0&&d2<=0&&d1>0) wn--;
    }
    return (bool)wn;
}

double PolygonArea(Point p[],int n){
    double s=0;
    for(int i=2;i<n;i++) s+=Cross(p[i]-p[1],p[i+1]-p[1]);
    return abs(s/2);
}

bool isConvex(Point poly[],int n){
    int last=0,now=0;
    for(int i=1;i<=n;i++){
        now=sgn(Cross(poly[i%n+1]-poly[i],poly[(i+1)%n+1]-poly[i%n+1]));
        if(last==0||now==0||now*last>0) last=now;
        else return false;
    }
    return true;
}

int ConvexHull(Point p[],int n,Point ch[]){//cannot handle repeat point
    sort(p+1,p+1+n);
    int m=0;
    for(int i=1;i<=n;i++){
        while(m>1&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--;
        ch[++m]=p[i];
    }
    int k=m;
    for(int i=n-1;i>=1;i--){//n-1
        while(m>k&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--;
        ch[++m]=p[i];
    }
    if(n>1) m--;//the first  rpoint
    return m;
}

double RotatingCalipers(Point p[],int n){
    if(n==1) return 0;
    if(n==2) return Len(p[1]-p[2]);
    int now=1;
    double ans=0;
    p[n+1]=p[1];
    for(int i=1;i<=n;i++){
        while(sgn(DisTL(p[now],p[i],p[i+1])-DisTL(p[now+1],p[i],p[i+1]))<=0) now=now%n+1;
        ans=max(ans,Len(p[now]-p[i]));
        ans=max(ans,Len(p[now]-p[i+1]));
    }
    return ans;
}

void iniPolygon(Point p[],int &n,double inf){
    n=0;
    p[++n]=Point(-inf,-inf);
    p[++n]=Point(inf,-inf);
    p[++n]=Point(inf,inf);
    p[++n]=Point(-inf,inf);
}
Point t[N];int tn;
void CutPolygon(Point p[],int &n,Point a,Point b){//get the left of a->b
    tn=0;
    Point c,d,e;
    for(int i=1;i<=n;i++){
        c=p[i],d=p[i%n+1];
        if(sgn(Cross(b-a,c-a))>=0) t[++tn]=c;
        if(isLSI(Line(a,b),Line(c,d))){
            e=LI(Line(a,b),Line(c,d));
            t[++tn]=e;
        }
    }
    n=tn;for(int i=1;i<=n;i++)p[i]=t[i];
}
//    iniPolygon(q,m,INF);
//    for(int i=1;i<=n;i++) CutPolygon(q,m,p[i%n+1],p[i]);

double minCircleCover(Point p[],int n,Point &c){
    random_shuffle(p+1,p+1+n);
    c=p[1];
    double r=0;
    for(int i=2;i<=n;i++)
        if(sgn(Len(c-p[i])-r)>0){
            c=p[i],r=0;
            for(int j=1;j<i;j++)
                if(sgn(Len(c-p[j])-r)>0){
                    c=(p[i]+p[j])/2,r=Len(c-p[i]);
                    for(int k=1;k<j;k++)
                        if(sgn(Len(c-p[k])-r)>0){
                            c=Circumcenter(p[i],p[j],p[k]);
                            r=Len(c-p[i]);
                        }
                }
        }
    return r;
}


//jie xi ji he
double a,b,c;
double f(Point p){return a*p.x+b*p.y+c;}
Point abcLI(Line l){
    double u=abs(f(l.s)),v=abs(f(l.t));
    return Point(l.s.x*v+l.t.x*u,l.s.y*v+l.t.y*u)/(u+v);
}

double F(double x){return x;} //function
inline double cal(double l,double r,double fl,double fr,double fm){
    return (fl+fr+4*fm)*(r-l)/6;
}
double Simpson(double l,double r,double now,double fl,double fr,double fm){
    double mid=(l+r)/2,flm=F((l+mid)/2),frm=F((mid+r)/2);
    double p=cal(l,mid,fl,fm,flm),q=cal(mid,r,fm,fr,frm);
    if(sgn(now-p-q)==0) return now;
    else return Simpson(l,mid,p,fl,fm,flm)+Simpson(mid,r,q,fm,fr,frm);
}

https://www.cnblogs.com/candy99/p/6360536.html

原文地址:https://www.cnblogs.com/Yinku/p/10357048.html