计算几何模板 - 大白书

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}//构造函数,方便函数编写
};
typedef Point Vector;//Vector只是Point的别名
Vector operator + (Vector A,Vector B) { return Vector (A.x+B.x,A.y+B.y); }
Vector operator - (Vector A,Vector 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); }
Vector operator < (const Point &A,const Point &B) { return A.x<B.x||(A.x==B.x&&A.y<B.y); }
Vector operator > (const Point &A,const Point &B) { return A.x>B.x||(A.x==B.x&&A.y>B.y); }
const double eps=1e-10;
int dcmp(double x)//三态函数,减少了精度问题
{
    if (fabs(x)<eps) { return 0; }
    else { return x<0?-1:1; }
} 
bool operator == (const Point &A,const Point &B) { return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0; }
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)
{
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal (Vector A)//计算单位法向量
{
    double L=Length(A);
    return Vector(-A.y/L,A.x/L);
}
Point GetSegmentIntersection(Point P,Vector V,Point Q,Vector w)//求线段交点
{
    Vector u= P - Q;
    double t=Cross(w,u)/Cross(V,w);
    return P+V*t;
}
Point GetLineIntersection(Point X1,Point Y1,Point X2,Point Y2)//求直线交点
{
    double a1 = (X1.y - Y1.y);
    double b1 = (Y1.x - X1.x);
    double c1 = (X1.x*Y1.y - Y1.x*X1.y);
    double a2 = (X2.y - Y2.y);
    double b2 = (Y2.x - X2.x);
    double c2 = (X2.x*Y2.y - Y2.x*X2.y);
    double x = (b1*c2 - b2*c1)/(a1*b2 - a2*b1);
    double y = (a2*c1 - a1*c2)/(a1*b2 - a2*b1);
    return Point(x,y);
}
double DistanceToLine(Point P,Point A,Point B)
{
    Vector v1=B-A,v2=P-A;
    return fabs(Cross(v1,v2)/Length(v1));
}
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);
    }
    else if (dcmp(Dot(v1,v2))>0)
    {
        return Length(v3);
    }
    else
    {
        return fabs(Cross(v1,v2))/Length(v1);
    }
}
Point GetLineProjection(Point P,Point A,Point B)//点在直线上的投影
{
    Vector v=B-A;
    return A+v*(Dot(v,P-A)/Dot(v,v));
}
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);
    return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0;
}
bool OnSemgment(Point p,Point a1,Point a2)
{
    return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
int main()
{

    return 0;
}

你可能感兴趣的:(模板,封装,计算几何)