• 计算几何模板初步


    #include <set>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1005;
    const double Pi = acos(-1.0);
    const double eps = 1e-6;
    const double INF = 1e12;
    inline int dcmp(double x) { //正负符号
    	if(x <= eps && x >= -eps) return 0;
    	return x > 0 ? 1 : -1;
    }
    inline double sqr(double x) { return x*x; } //平方
    struct Vector { //向量
    	double x, y;
    	inline Vector(double _x=0, double _y=0): x(_x), y(_y){}
    	inline Vector operator *(const double &k)const { return Vector(x*k, y*k) ;}
    	inline Vector operator +(const Vector &t)const { return Vector(x+t.x, y+t.y); }
    	inline Vector operator -(const Vector &t)const { return Vector(x-t.x, y-t.y); }
    };
    struct Point { //点
        double x, y;
        inline Point(double _x=0, double _y=0): x(_x), y(_y){}
        inline Vector operator -(const Point &t)const {  return Vector(x - t.x, y - t.y); }
        inline Point operator +(const Vector &t)const {  return Point(x + t.x, y + t.y); }
        inline double dist(const Point &t) { return sqrt(sqr(x-t.x) + sqr(y-t.y)); }
        inline double dist(const double &a, const double &b) { return sqrt(sqr(x-a) + sqr(y-b)); }
    };
    struct Line { //线
    	Point p;
    	Vector v; //有向
    	double ang;
    	inline Line(Point _p=Point(0, 0), Vector _v=Vector(0, 0)): p(_p), v(_v), ang(atan2(v.y, v.x)){}
    	inline bool operator <(const Line &t)const { return ang < t.ang; }
    };
    struct Circle { //圆
    	Point o; double r;
    	inline Circle(Point _o=Point(0, 0), double _r=0): o(_o), r(_r){}
    };
    inline double Cross(const Vector &a, const Vector &b) { return a.x*b.y - a.y*b.x; } //叉积
    inline double Dot(const Vector &a, const Vector &b) { return a.x*b.x + a.y*b.y; } //点积
    inline bool Turn_Left(const Point &a, const Point &b, const Point &c) { return dcmp(Cross(b-a, c-a)) > 0; } //三点的上凸或下凸情况
    inline bool On_Left(const Line &a, const Point &b) { return dcmp(Cross(a.v, b-a.p)) >= 0; } //点对于线的位置
    inline Point GLI(const Point &P, const Vector &v, const Point &Q, const Vector &w) { //直线交点
    	Vector u = Q - P;
    	double k = Cross(u, w) / Cross(v, w);
    	return P + v*k;
    }
    inline double Angle_C(const double &a, const double &b, const double &c) { //余弦定理
    	return acos((sqr(a)+sqr(b)-sqr(c))/(2*a*b));
    	//cos C = (a^2+b^2-c^2)/(2ab)
    }
    inline double Tri_S(const Point &a, const Point &b, const Point &c) { //三角形的面积
    	return fabs(Cross(b-a, c-a))/2;
    }
    inline Point rotate(const double &x, const double &y, const double &degree) { //逆时针旋转degree弧度的坐标
    	return Point(x*cos(degree)-y*sin(degree), x*sin(degree)+y*cos(degree));
    }
    int n, cur; Point O[MAXN], poly[MAXN];
    inline bool cmp(const Point &a, const Point &b) { return dcmp(a.x-b.x) ? a.x < b.x : a.y < b.y; }
    inline void  Convex_hull() { //凸包
    	sort(O + 1, O + n + 1, cmp);
    	int tmp;
    	poly[++cur] = O[tmp=1];
    	for(int i = 2; i <= n; ++i) { //下凸壳
    		while(cur > tmp && !Turn_Left(poly[cur-1], poly[cur], O[i])) --cur;
    		poly[++cur] = O[i];
    	}
    	tmp = cur;
    	for(int i = n-1; i > 1; --i) { //上凸壳
    		while(cur > tmp && !Turn_Left(poly[cur-1], poly[cur], O[i])) --cur;
    		poly[++cur] = O[i];
    	}
    	while(cur > tmp && !Turn_Left(poly[cur-1], poly[cur], O[1])) --cur;
    	return;
    }
    
    //stO   Orz  -.-  >.<  xp   XD   qwq  QAQ   ToT  QwQ  QuQ  :-)  )-: TnT
    inline double calc(double len, double fL, double fM, double fR) {
    	return (fL + 4*fM + fR) * len / 6; //辛普森公式
    }
    inline double f(double x) { /*...*/ } //求值
    inline double Simpson(double L, double M, double R, double fL, double fM, double fR, double tmp) { //辛普森
    	double M1 = (L + M) / 2, M2 = (M + R) / 2;
    	double fM1 = f(M1), fM2 = f(M2);
    	double g1 = calc(M-L, fL, fM1, fM), g2 = calc(R-M, fM, fM2, fR);
    	if(dcmp(tmp-g1-g2) == 0) return g1 + g2;
    	return Simpson(L, M1, M, fL, fM1, fM, g1) + Simpson(M, M2, R, fM, fM2, fR, g2);
    }
    inline double Farthest_dist() { //旋转卡壳求最远点对
    	if(cur == 2) return poly[1].dist(poly[2]);
    	for(int i = 0; i < cur; ++i) poly[i] = poly[i+1];
    	double res = 0;
    	for(int i = 0, j = 2; i < cur; ++i) {
    		while(Tri_S(poly[i], poly[(i+1)%cur], poly[j]) < Tri_S(poly[i], poly[(i+1)%cur], poly[(j+1)%cur])) j = (j+1)%cur;
    		res = max(res, poly[i].dist(poly[j]));
    	}
    	for(int i = cur; i >= 1; --i) poly[i] = poly[i-1];
    	return res;
    }
    int main () {}
    
  • 相关阅读:
    JavaScript 正则表达式
    git常用命令
    用纯css使内容永远居在页面底部
    Oracle中随机抽取N条记录
    表数据回复到某个时候
    oracle同名存储过程被覆盖后如何恢复(转)
    mybatis+spring+mysql
    定位
    关于js的闭包和复制对象
    idea展示runDashboard的窗口
  • 原文地址:https://www.cnblogs.com/Orz-IE/p/12039440.html
Copyright © 2020-2023  润新知