【计算几何板子】

【计算几何板子】

初始板子

遇到计算几何的题目就用这个模版来写

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define whiteink signed main
#define fi first
#define sc second

//初始化

//字符串读入浮点数
const int Knum=4;
int read(int k=Knum){
    string s;
    cin>>s;
    int num=0;
    int it=s.find('.');
    if(it!=-1){//存在小数点
        num=s.size()-it-1;//计算小数位数
        s.erase(s.begin()+it);//删除小数点
    }
    for(int i=1;i<=k-num;i++){
        s+='0';
    }
    return stoi(s);
}

//预置函数
using ld=long double;
const ld PI=acos(-1);
const ld EPS=1e-7;
const ld INF=numeric_limits<ld>::max();
#define cc(x) cout<<fixed<<setprecision(x);
ld fgcd(ld x,ld y){
    return abs(y)<EPS?abs(x):fgcd(y,fmod(x,y));
}
template<class T,class S> bool equal(T x,S y){
    return -EPS<x-y && x-y<EPS;
}
template<class T> int sign(T x){
    if(-EPS<x && x<EPS) return 0;
    return x<0?-1:1;
}

//点封装
template<class T> struct Point{//vector尽量使用push_back
    T x,y;
    //初始化
    Point(T x_=0,T y_=0) : x(x_),y(y_){}
    template<class U> operator Point<U>(){//自动类型匹配
        return Point<U>(U(x),U(y));
    }
    Point &operator+=(Point p) & { return x+=p.x,y+=p.y,*this;}
    Point &operator+=(T t) & { return x+=t,y+=t,*this;}
    Point &operator-=(Point p) & { return x-=p.x,y-=p.y,*this;}
    Point &operator-=(T t) & { return x-=t,y-=t,*this;}
    Point &operator*=(T t) & { return x*=t,y*=t,*this;}
    Point &operator/=(T t) & { return x/=t,y/=t,*this;}
    Point operator-() const { return Point(-x,-y);}
    friend Point operator+(Point a,Point b){ return a+=b;}
    friend Point operator+(Point a,T b){ return a+=b;}
    friend Point operator-(Point a,Point b){ return a-=b;}
    friend Point operator-(Point a,T b){ return a-=b;}
    friend Point operator*(Point a,T b){ return a*=b;}
    friend Point operator*(T a,Point b){ return b*=a;}
    friend Point operator/(Point a,T b){ return a/=b;}
    friend bool operator<(Point a,Point b){
        return equal(a.x,b.x) ? a.y<b.y-EPS : a.x<b.x-EPS;
    }
    friend bool operator>(Point a,Point b){ return b<a; }
    friend bool operator==(Point a,Point b){ return !(a<b) && !(b<a);}
    friend bool operator!=(Point a,Point b){ return a<b || b<a;}
    friend auto &operator>>(istream &is,Point &p){
        return is>>p.x>>p.y;
    }
    friend auto &operator<<(ostream &os,Point p){
        return os<<"("<<p.x<<","<<p.y<<")";
    }
};

//线封装
template<class T> struct Line{
    Point<T> a,b;
    Line(Point<T> a_=Point<T>(),Point<T> b_=Point<T>()): a(a_),b(b_){}
    template<class U> operator Line<U>(){//自动类型匹配
        return Line<U>(Point<U>(a),Point<U>(b));
    }
    friend auto &operator<<(ostream &os,Line l){
        return os<<"<"<<l.a<<","<<l.b<<">";
    }
};

const int N=3e5+10;
int n;

void solve(){
    
    
}

whiteink(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--) solve();
    return 0;
}

库函数类实现

利用内置complex类实现
会和自己写的冲突(一般不用)

//库函数类实现(双精度):利用complex复数类【会和自己写的冲突】
using Real=int;
using Point = complex<Real>;
Real cross(const Point &a,const Point &b){
    return (conj(a)*b).imag();
}
Real dot(const Point &a,const Point &b){
    return (conj(a)*b).real();
}
posted @ 2025-07-19 22:06  White_ink  阅读(15)  评论(0)    收藏  举报