不完整,待补充
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-10; 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-( 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 ); } 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.x - B.x ) == 0 && dcmp( A.y - B.y ) < 0 ); } bool operator>( const Point& A, const Point& B ) //两点比较大于 { return dcmp( A.x - B.x) > 0 || ( dcmp(A.x - B.x ) == 0 && dcmp( A.y - B.y ) > 0 ); } 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 toRad( double deg ) //角度转弧度 { return deg / 180.0 * acos( -1.0 ); } int ConvexHull( Point *p, int n, Point *ch ) //求凸包,卷包裹法,O(n2) { sort( p, p + n ); 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; } 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; }
graham算法求凸包
//求凸包,graham算法,O(nlogn),返回凸包点的个数 int graham( Point *p, int n, Point *ch ) { if ( n <= 2 ) return 0; int top = 0; sort( p, p + n ); ch[ top ] = p[0]; ch[ ++top ] = p[1]; ch[ ++top ] = p[2]; top = 1; for ( int i = 2; i < n; ++i ) { while ( top && dcmp( Cross( ch[top] - ch[top - 1], p[i] - ch[top - 1] ) ) <= 0 ) --top; ch[++top] = p[i]; } int len = top; ch[++top] = p[n - 2]; for ( int i = n - 3; i >= 0; --i ) { while ( top > len && dcmp( Cross( ch[top] - ch[top - 1], p[i] - ch[top - 1] ) ) <= 0 ) --top; ch[++top] = p[i]; } return top; }
关于圆的一些模板(不完整,待补充)
struct Circle { Point c; //圆心坐标 double r; //半径 Circle() {} Circle( Point c, double r ): c(c), r(r) {} Point getPoint( double theta ) //根据极角返回圆上一点的坐标 { return Point( c.x + cos(theta)*r, c.y + sin(theta)*r ); } void readCircle() { scanf("%lf%lf%lf", &c.x, &c.y, &r ); return; } }; //过定点做圆的切线,得到切点,返回切点个数 //tps保存切点坐标 int getTangentPoints( Point p, Circle C, Point *tps ) { int cnt = 0; double dis = sqrt( PointDis( p, C.c ) ); int aa = dcmp( dis - C.r ); if ( aa < 0 ) return 0; //点在圆内 else if ( aa == 0 ) //点在圆上,该点就是切点 { tps[cnt] = p; ++cnt; return cnt; } //点在圆外,有两个切点 double base = atan2( p.y - C.c.y, p.x - C.c.x ); double ang = acos( C.r / dis ); //printf( "base = %f ang=%f ", base, ang ); //printf( "base-ang=%f base+ang=%f ", base - ang, base + ang ); tps[cnt] = C.getPoint( base - ang ), ++cnt; tps[cnt] = C.getPoint( base + ang ), ++cnt; return cnt; } //求两圆外公切线切点,返回切线个数 //p是圆c2在圆c1上的切点 int makeCircle( Circle c1, Circle c2, Point *p ) { int cnt = 0; double d = sqrt( PointDis(c1.c, c2.c) ), dr = c1.r - c2.r; double b = acos(dr / d); double a = atan2( c2.c.y - c1.c.y, c2.c.x - c1.c.x ); double a1 = a - b, a2 = a + b; p[cnt++] = Point(cos(a1) * c1.r, sin(a1) * c1.r) + c1.c; p[cnt++] = Point(cos(a2) * c1.r, sin(a2) * c1.r) + c1.c; return cnt; } //求三角形的外心 Point GetMid( Point *p ) { Point tmp1 = p[0] + ( p[1] - p[0] ) / 2.0; Point tmp2 = p[1] + ( p[2] - p[1] ) / 2.0; Vector v1 = Normal( p[1] - p[0] ); Vector v2 = Normal( p[2] - p[1] ); return GetLineIntersection( tmp1, v1, tmp2, v2 ); }