ACM计算几何总结

这篇博客详细总结了ACM中的计算几何问题,包括点与向量、线、三角形、多边形、圆的处理,以及凸包、半平面交、最近点对、旋转卡壳等算法,最后探讨了网格图的Pick定理和多边形与网格点的关系。
摘要由CSDN通过智能技术生成

1 几何基础

#include <cstdio>
#include <cmath>
using namespace std;
const double pi = acos(-1.0);
const double inf = 1e100;
const double eps = 1e-6;
int sgn(double d){
    
    if(fabs(d) < eps)
        return 0;
    if(d > 0)
        return 1;
    return -1;
}
int dcmp(double x, double y){
    
    if(fabs(x - y) < eps)
        return 0;
    if(x > y)
        return 1;
    return -1;
}
int main() {
    
    double x = 1.49999;
    int fx = floor(x);//向下取整函数
    int cx = ceil(x);//向上取整函数
    int rx = round(x);//四舍五入函数
    printf("%f %d %d %d\n", x, fx, cx, rx);
    //输出结果 1.499990 1 2 1
    return  0 ;
}

2 点与向量

2.1 手动实现

struct Point{
    
    double x, y;
    Point(double x = 0, double y = 0):x(x),y(y){
    }
};
typedef Point Vector;
Vector operator + (Vector A, Vector B){
    
    return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Point A, Point B){
    
    return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p){
    
    return Vector(A.x*p, A.y*p);
}
Vector operator / (Vector A, double p){
    
    return Vector(A.x/p, A.y/p);
}
bool operator < (const Point& a, const Point& b){
    
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}
const double eps = 1e-6;
int sgn(double x){
    
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
bool operator == (const Point& a, const Point& b){
    
    if(sgn(a.x-b.x) == 0 && sgn(a.y-b.y) == 0)
        return true;
    return false;
}
double Dot(Vector A, Vector B){
    
    return A.x*B.x + A.y*B.y;
}
double Length(Vector A){
    
    return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B){
    
    return acos(Dot(A, B)/Length(A)/Length(B));
}
double Cross(Vector A, Vector B){
    
    return A.x*B.y-A.y*B.x;
}
double Area2(Point A, Point B, Point C){
    
    return Cross(B-A, C-A);
}
Vector Rotate(Vector A, double rad){
    //rad为弧度 且为逆时针旋转的角
    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A){
    //向量A左转90°的单位法向量
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}
bool ToLeftTest(Point a, Point b, Point c){
    
    return Cross(b - a, c - b) > 0;
}

2.2 复数黑科技

#include <complex>
using namespace std;
typedef complex<double> Point;
typedef Point Vector;//复数定义向量后,自动拥有构造函数、加减法和数量积
const double eps = 1e-9;
int sgn(double x){
    
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
double Length(Vector A){
    
    return abs(A);
}
double Dot(Vector A, Vector B){
    //conj(a+bi)返回共轭复数a-bi
    return real(conj(A)*B);
}
double Cross(Vector A, Vector B){
    
    return imag(conj(A)*B);
}
Vector Rotate(Vector A, double rad){
    
    return A*exp(Point(0, rad));//exp(p)返回以e为底复数的指数
}

3 点与线

3.1 直线定义

struct Line{
    //直线定义
    Point v, p;
    Line(Point v, Point p):v(v), p(p) {
    }
    Point point(double t){
    //返回点P = v + (p - v)*t
        return v + (p - v)*t;
    }
};

3.2 求两直线交点

//调用前需保证 Cross(v, w) != 0
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
    
    Vector u = P-Q;
    double t = Cross(w, u)/Cross(v, w);
    return P+v*t;
}

3.3 求点到直线距离

//点P到直线AB距离
double DistanceToLine(Point P, Point A, Point B){
    
    Vector v1 = B-A, v2 = P-A;
    return fabs(Cross(v1, v2)/Length(v1));
}//不去绝对值,得到的是有向距离

3.4 求点到线段距离

//点P到线段AB距离公式
double DistanceToSegment(Point P, Point A, Point B){
    
    if(A == B)
        return Length(P-A);
    Vector v1 = B-A, v2 = P-A, v3 = P-B;
    if(dcmp(Dot(v1, v2)) < 0)
        return Length(v2);
    if(dcmp(Dot(v1, v3)) > 0)
        return Length(v3);
    return DistanceToLine(P, A, B);
}

3.5 求点在直线上的投影点

//点P在直线AB上的投影点
Point GetLineProjection(Point P, Point A, Point B){
    
    Vector v = B-A;
    return A+v*(Dot(v, P-A)/Dot(v, v));
}

3.6 判断点是否在线段上

//判断p点是否在线段a1a2上
bool OnSegment(Point p, Point a1, Point a2){
    
    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}

3.7 判断两线段是否相交

//判断两线段是否相交
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
    
    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);
    //if判断控制是否允许线段在端点处相交,根据需要添加
    if(!sgn(c1) || !sgn(c2) || !sgn(c3) || !sgn(c4)){
    
        bool f1 = OnSegment(b1, a1, a2);
        bool f2 = OnSegment(b2, a1, a2);
        bool f3 = OnSegment(a1, b1, b2);
        bool f4 = OnSegment(a2, b1, b2);
        bool f = (f1|f2|f3|f4);
        return f;
    }
    return (sgn(c1)*sgn(c2) < 0 && sgn(c3)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值