计算几何 学习笔记

模板及讲解

计算几何大坑

知识点

向量点积

几何方法:$\vec a \cdot \vec b=|\vec a||\vec b| \cos \theta$
坐标系:$\vec a(x_1,y_1), \vec b(x_2,y_2),​$则$\vec a \cdot \vec b=x_1x_2+y_1y_2​$

判垂直:$\vec a ⊥ \vec b \Leftrightarrow \vec a \cdot \vec b = \vec 0$

向量叉积

几何方法:$\vec a \times \vec b=|\vec a||\vec b| \sin \theta$
坐标系:$\vec a(x_1,y_1), \vec b(x_2,y_2),$则$\vec a \times \vec b=x_1y_2-x_2y_1$

判共线:$\vec a ∥ \vec b \Leftrightarrow \vec a \times \vec b = \vec 0$

向量旋转

设$\vec a(x, y)$,逆时针转角为$\theta$,则旋转后的向量为$(x\cos \theta-y \sin \theta, x \sin \theta+y \cos \theta)$

代码

预备

const double eps = 1e-6, pi = acos(-1.0);
//Header
inline int dcmp(double x) { // 比较大小
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
inline double sqr(double x) { // x 平方
return x * x;
}


//Point
struct Point { // 点
double x, y;
Point(double _x = 0, double _y = 0): x(_x), y(_y) {};
inline void in() {scanf("%lf%lf",&x, &y);}
};
inline Point operator + (Point A, Point B) {
return Point(A.x + B.x, A.y + B.y);
}
inline Point operator - (Point A, Point B) {
return Point(A.x - B.x, A.y - B.y);
}
inline Point operator * (Point A, double p) {
return Point(A.x * p, A.y * p);
}
inline Point operator / (Point A, double p) { // 向量(点)加减乘除
return Point(A.x / p, A.y / p);
}
inline bool operator < (const Point &a, const Point &b) { // 点位置判断
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
inline bool operator == (const Point &a, const Point &b) { // 点是否重合
return !dcmp(a.x - b.x) && !dcmp(a.y - b.y);
}
inline double Distance(Point A, Point B) { // 两点距离
return sqrt(sqr(A.x - B.x) + sqr(A.y - B.y));
}
inline double Dot(Point A, Point B) { // 点积
return A.x * B.x + A.y * B.y;
}
inline double Norm(Point A) {
return A.x * A.x + A.y * A.y;
}
inline double Length(Point A) { // 向量模
return sqrt(Dot(A, A));
}
inline double GetAngle(Point A,Point B) { // 求向量夹角
return acos(Dot(A, B)) / Length(A) / Length(B);
}
inline double GetAngle(Point v) { // 极角
return atan2(v.y, v.x);
}
inline double Cross(Point A, Point B) { // 叉积
return A.x * B.y - A.y * B.x;
}
inline Point Unit(Point x) {
return x / Length(x);
}
inline Point Normal(Point x) { // 向量的单位法向量
return Point(-x.y, x.x) / Length(x);
}
inline Point Rotate(Point A, double rad) { // 向量绕起点旋转
return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
inline double Area2(const Point &A, const Point &B, const Point &C) { // 平行四边形面积
return Cross(B - A, C - A);
}
inline void getLineGeneralEquation(const Point &p1, const Point &p2, double &a, double &b, double &c) {
a = p2.y - p1.y;
b = p1.x - p2.x;
c = -a * p1.x - b * p1.y;
}
inline Point GetLineIntersection(Point P, Point v, Point Q, Point w) {
Point u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
inline double DistanceToLine(Point P, Point A, Point B) {
Point v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1);
}
inline double DistanceToSegment(Point P, Point A, Point B) {
if (A == B)return Length(P-A);
Point v1 = B - A,v2 = P - A, v3 = P - B;
if (dcmp(Dot(v1, v2)) < 0) return Length(v2);
else if (dcmp(Dot(v1, v3)) > 0) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
}
inline Point GetLineProjection(Point P, Point A, Point B) {
Point v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
}
inline bool OnSegment(Point p, Point a1, Point a2) {
if(a1 == p || a2 == p) return 1;
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p,a2 - p)) < 0;
}
inline bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
if (OnSegment(a1, b1, b2) || OnSegment(a2, b1, b2) || OnSegment(b1, a1, a2) || OnSegment(b2, a1, a2)) return 1;
double c1 = Cross(a2 - a1, b1 - a1),c2 = Cross(a2 - a1, b2 - a1);
double c3 = Cross(b2 - b1, a1 - b1),c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
inline double PolygonArea(Point *p, int n) {
double area = 0.0;
for (int i = 1; i < n - 1; i++) area += Cross(p[i] - p[0], p[i+1] - p[0]);
return area / 2.0;
}
inline Point GetMidPoint(Point A, Point B) {
return Point((A.x + B.x) / 2.0, (A.y + B.y) / 2.0);
}
inline int PointinPolygon(Point p, vector<Point> poly) {
int n = poly.size(), flg = 0;
for(int i = 0; i < n; i++) {
if(OnSegment(p, poly[i], poly[(i + 1)% n])) return -1;
int k = dcmp(Cross(poly[(i + 1) % n] - poly[i], p - poly[i]));
int d1 = dcmp(poly[i].y - p.y);
int d2 = dcmp(poly[(i + 1) % n].y - p.y);
if(k > 0 && d1 <= 0 && d2 > 0) flg++;
if(k < 0 && d2 <= 0 && d1 > 0) flg++;
}
if(flg) return 1;
return 0;
}

常见题型

------ 本文结束 ------