计算几何template

//template

const double eps=1e-7;
const double pi=3.14159265;
struct Point
{
    double x, y;
    Point( double x = 0, double y = 0 ):x(x), y(y) { }
};
typedef Point Vector; 
struct Circle
{
    Point c;
    Circle(){}
    Circle(Point c,double r):c(c),r(r){}
    double r;
    Point point(double a)
    {
        return Point(c.x + cos(a)*r,c.y + sin(a)*r);
    }
}c;
 
struct Line
{
    Point p;
    Vector v;
    double ang;
    Line(){}
    Line(Point p,Vector v):p(p),v(v)
    {
        ang = atan2(v.y,v.x);
    }
    Point point(double t)
    {
        return Point(p.x+v.x*t,p.y+v.y*t);
    }
    bool operator < (const Line& L) const
    {
        return ang < L.ang;
    }
};
 
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 );
}
 
bool operator<( const Point& A, const Point& B )   //两点比较
{
    return A.x < B.x || ( A.x == B.x && A.y < B.y );
}
 
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 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;
}
 
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, v3) ) > 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 ),
                c3 = Cross( b2 - b1, a1 - b1 ), c4 = Cross( b2 - b1, a2 - b1 );
    return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
 
bool OnSegment( Point p, Point a1, Point a2 )   //点在线段上,不包含端点
{
    return dcmp( Cross(a1 - p, a2 - p) ) == 0 && dcmp( Dot( a1 - p, a2 - p ) ) < 0;
}
 
double PolygonArea( Point *p, int n )   //多边形有向面积
{
    double area = 0;
    for ( int i = 1; i < n - 1; ++i )
        area += Cross( p[i] - p[0], p[i + 1] - p[0] );
    return area / 2.0;
}
vector<Point> sol;
int getLineCircleIntersection(Line L,Circle C)  //直线和圆的交点
{
    double a = L.v.x,b = L.p.x - C.c.x,c = L.v.y,d = L.p.y - C.c.y;
    double e = a*a + c*c,f = 2*(a*b + c*d),g = b*b + d*d - C.r*C.r;
    double delta = f*f - 4*e*g;
    if(dcmp(delta) < 0)
        return 0;
    double t1,t2;
    if(dcmp(delta) == 0)
    {
        t1 = t2 = -f/(2*e);
        sol.push_back(L.point(t1));
        return 1;
    }
 
    t1 = (-f - sqrt(delta)) / (2*e);
    sol.push_back(L.point(t1));
    t2 = (-f + sqrt(delta)) / (2*e);
    sol.push_back(L.point(t2));
    return 2;
}
 
double angle(Vector v)                 //计算向量极角
{
    return atan2(v.y,v.x);
}
 
int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point>& sol)    //计算两圆相交
{
    double d = Length(C1.c - C2.c);
    if(dcmp(d) == 0)
    {
        if(dcmp(C1.r - C2.r) == 0)
            return -1;
        return 0;
    }
    if(dcmp(C1.r + C2.r - d) < 0)
        return 0;
    if(dcmp(fabs(C1.r-C2.r) - d) > 0)
        return 0;
 
    double a = angle(C2.c - C1.c);
    double da = acos(C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d);
    Point p1 = C1.point(a-da),p2 = C1.point(a+da);
 
    sol.push_back(p1);
    if(p1 == p2)
        return 1;
    sol.push_back(p2);
    return 2;
}
 
int getTangent(Point p,Circle C,Vector* v)               //过定点做圆的切线
{
    Vector u = C.c - p;
    double dist = Length(u);
    if(dist < C.r)
        return 0;
    else if(dcmp(dist - C.r) == 0)
    {
        v[0] = Rotate(u,pi/2);
        return 1;
    }
    else
    {
        double ang = asin(C.r / dist);
        v[0] = Rotate(u,-ang);
        v[1] = Rotate(u,ang);
        return 2;
    }
}
 
int getTangents(Circle A,Circle B,Point* a,Point* b)                      //求两圆公切线
{
    int cnt = 0;
    if(A.r < B.r)
    {
        swap(A,B);
        swap(a,b);
    }
    int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
    int rdiff = A.r - B.r;
    int rsum = A.r + B.r;
    if(d2 < rdiff*rdiff)
        return 0;
 
    double base = atan2(B.c.y-A.c.y,B.c.x-A.c.x);
    if(d2 == 0 && A.r == B.r)
        return -1;
    if(d2 == rdiff*rdiff)
    {
        a[cnt] = A.point(base);
        b[cnt] = B.point(base);
        cnt++;
        return 1;
    }
    double ang = acos((A.r - B.r) / sqrt(d2));
    a[cnt] = A.point(base+ang);
    b[cnt] = B.point(base+ang);
    cnt++;
    a[cnt] = A.point(base-ang);
    b[cnt] = B.point(base-ang);
    cnt++;
    if(d2 == rsum*rsum)
    {
        a[cnt] = A.point(base);
        b[cnt] = B.point(pi+base);
        cnt++;
    }
    else if(d2 > rsum*rsum)
    {
        double ang = acos((A.r+B.r) / sqrt(d2));
        a[cnt] = A.point(base+ang);
        b[cnt] = B.point(pi+base+ang);
        cnt++;
        a[cnt] = A.point(base-ang);
        b[cnt] = B.point(pi+base-ang);
        cnt++;
    }
    return cnt;
}
 
double torad( double deg )   //角度转弧度
{
    return deg / 180.0 * acos( -1.0 );
}
 
void get_coord(double R,double lat,double Ing,double& x,double& y,double& z)   //经纬度(角度)转化为空间坐标
{
    lat = torad(lat);
    Ing = torad(Ing);
    x = R*cos(lat)*cos(Ing);
    y = R*cos(lat)*sin(Ing);
    z = R*sin(lat);
}
 
int ConvexHull( Point *p, int n, Point *ch )    //求凸包
{
    sort( p, p + n );
    n = unique( p, p + n ) - p;
    int m = 0;
    for ( int i = 0; i < n; ++i )
    {
        while ( m > 1 && Cross( ch[m - 1] - ch[m - 2], p[i] - ch[m - 2] ) <= 0 ) --m;
        ch[m++] = p[i];
    }
 
    int k = m;
    for ( int i = n - 2; i >= 0; --i )
    {
        while ( m > k && Cross( ch[m - 1] - ch[m - 2], p[i] - ch[m - 2] ) <= 0 ) --m;
        ch[m++] = p[i];
    }
 
    if ( n > 1 )
        --m;
    return
        m;
}
 
int isPointInPolygon( Point p, Point *poly, int n )   //判断一点是否在凸包内
{
    int wn = 0;
 
    for ( int i = 0; i < n; ++i )
    {
        Point& p1 = poly[i], p2 = poly[ (i + 1)%n ];
        if ( p == p1 || p == p2 || OnSegment( p, p1, p2 ) ) return -1;  //在边界上
        int k = dcmp( Cross( p2 - p1, p - p1 ) );
        int d1 = dcmp( p1.y - p.y );
        int d2 = dcmp( p2.y - p.y );
        if ( k > 0 && d1 <= 0 && d2 > 0 ) ++wn;
        if ( k < 0 && d2 <= 0 && d1 > 0 ) --wn;
    }
 
    if ( wn ) return 1;   //内部
    return 0;             //外部
}
 
 
bool checkConvexHullIntersection( Point *a, Point *b, int na, int nb )             //判断凸包是否相交
{
    for ( int i = 0; i < na; ++i )
        if ( isPointInPolygon( a[i], b, nb ) ) return true;
 
    for ( int i = 0; i < nb; ++i )
        if ( isPointInPolygon( b[i], a, na ) ) return true;
 
    for ( int i = 0; i < na; ++i )
        for ( int j = 0; j < nb; ++j )
            if ( SegmentProperIntersection(a[i], a[ (i + 1) % na ], b[j], b[ (j + 1) % nb ] ) ) return true;
 
    return false;
}


posted on 2020-10-10 12:57  xlinsist  阅读(129)  评论(0编辑  收藏  举报