计算几何 点积叉积 点类模板

模板摘自:ACM国际大学生程序设计竞赛 算法与实现 计算几何篇

计算几何

3.1多边形

3.1.1几何误差

const double eps = 1e-8;
int cmp (double x) {
    if (fabs(x) < eps) return 0;
    if (x > 0) return 1;
    return -1;
}

3.1.2计算几何点类

函数 用法
double sqr(double x) 计算平方
double det(const point &a, const point &b) 计算两个向量的叉积
double dot(const point &a, const point &b) 计算两个向量的点积
double dist(const point &a, const point &b) 计算两个点的距离
point rotate_point(const point &p, double A) 向量op绕原点逆时针旋转A(弧度)

点积

几何意义:|AC|*|AD|
A B → ∗ A C → = ∣ A B ∣ ⋅ ∣ A C ∣ ⋅ c o s θ \overrightarrow{AB}*\overrightarrow{AC} = |AB|\cdot|AC|\cdot cos \theta AB ∗AC =∣AB∣⋅∣AC∣⋅cosθ
计算几何 点积叉积 点类模板

如果点积是正的,那就代表在1,4象限

负的就代表在2,3象限

叉积

几何意义:|AC|*|BD|,即三角形abc面积的两倍
A B → ∗ A C → = ∣ A B ∣ ⋅ ∣ A C ∣ ⋅ s i n θ \overrightarrow{AB}*\overrightarrow{AC} = |AB|\cdot|AC|\cdot sin \theta AB ∗AC =∣AB∣⋅∣AC∣⋅sinθ
如果是正的,就代表在12象限

负的就是34象限

const double pi = acos(-1.0);
inline double sqr (double x) {
    return x*x;
}
struct point {
    double x, y;
    point() {}
    point(double x, double y) : x(a), y(b) {}
    void input() {
        scanf("%lf%lf", &x, &y);
//        cin>>x>>y;
    }
    friend point operator + (const point &a, const point &b) {
		return point(a.x + b.x, a.y + b.y);
	}
	friend point operator - (const point &a, const point &b) {
		return point(a.x - b.x, a.y - b.y);
	}
	friend bool operator == (const point &a, const point &b) {
		return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
	}
	friend point operator * (const point &a, const double &b) {
		return point(a.x * b, a.y * b);
	}
	friend point operator * (const double &a, const point &b) {
		return point(a * b.x, a * b.y);
	}
	friend point operator / (const point &a, const double &b) {
		return point(a.x / b, a.y / b);
	}
	double norm() {//计算长度
		return sqrt(sqr(x) + sqr(y));
	}
}
//计算两个向量的叉积
double det (const point &a, const point &b) {
    return a.x*b.y - a.y*b.x;
}
//计算两个向量的点积
double dot (const point &a, const point &b) {
    return a.x*b.x + a.y*b.y;
}
double dist (const point &a, const point &b) {
    return (a - b).norm();
}
double rotate_point (const point &p, double A) {
    double tx = p.x, ty = p.y;
    return point (tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A));
}
上一篇:Codeforces Round #742 (Div. 2) AB


下一篇:CG.DYJ-顺序表比较