大白二維計算幾何模板

2021-06-21 09:21:48 字數 4404 閱讀 4793

#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;

#define inf 0x3f3f3f3f

#define eps 1e-7

#define ll long long

#define ull unsigned long long

#define mp make_pair

#define pb push_back

#define ls ( i << 1 )

#define rs ( ls | 1 )

#define md ( ( ll[i] + rr[i] ) >> 1 )

#define mxn 5020

#define pi acos( -1.0 )

// 角度轉換為弧度

double torad( double deg )

int dcmp( double x )

struct point

point operator + ( const point &b ) const

point operator - ( const point &b ) const

point operator * ( const double &k ) const

point operator / ( const double &k ) const

bool operator < ( const point &b ) const

bool operator == ( const point &b ) const

double len()

};struct line

line( point v, point p ): v( v ), p( p )

point get_point( double t )

bool operator < ( const line &l ) const

};// 圓

struct circle

circle( point c, double r ): c( c ), r( r ){}

//圓心角為a的點

point get_point( double a )

};double cross( point a, point b )

double dot( point a, point b )

// 極角

double angle( point v )

double angle( point a, point b )

double area( point a, point b, point c )

point rotate( point a, double rad )

point normal( point a )

// 直線的交點,p 和 q是直線上的點, v和w是方向向量

point getlineintersection( point p, point v, point q, point w )

// 點到直線的距離

double distoline( point p, point a, point b )

// 點到線段的距離

double distoseg( point p, point a, point b )

// 點在直線的投影

point getlinepro( point p, point a, point b )

// 線段規範相交

bool segprointers( point a1, point a2, point b1, point b2 )

bool ons( point p, point a1, point a2 )

//多邊形有向面積

double polygonarea( point *p, int n )

int getlinecirins( line l, circle c, double &t1, double &t2, vector&sol )

t1 = ( - f - sqrt( delta ) ) / ( 2 * e );

sol.push_back( l.get_point( t1 ) );

t2 = ( - f + sqrt( delta ) ) / ( 2 * e );

sol.push_back( l.get_point( t2 ) );

return 2;

}// 兩圓相交

int getccins( circle c1, circle c2, vector&sol )

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 * c2.r * d ) );

point p1 = c1.get_point( a - da ), p2 = c1.get_point( a + da );

sol.push_back( p1 );

if( p1 == p2 )

return 1;

sol.push_back( p2 );

return 2;

}// 過點p到圓c的切線,v[i]是第i條切線。返回切線條數

int gettangents( point p, circle c, point *v )

double ang = asin( c.r / dist );

v[0] = rotate( u, - ang );

v[1] = rotate( u, + ang );

return 2;

}// 求兩圓的切線,返回切線條數,-1表示無窮條,

// a[i] 和 b[i]分別是第i條切線在圓a和圓b上的切點

int gettangents( circle a, circle b, point *a, point *b )

double ang = acos( ( a.r - b.r ) / sqrt( d2 ) );

a[cnt] = a.get_point( base + ang );

b[cnt++] = b.get_point( base + ang );

a[cnt] = a.get_point( base - ang );

b[cnt++] = b.get_point( base - ang );

if( dcmp( d2 - rsum * rsum ) == 0 )

else

} return cnt;

}// 點在多邊形內判定, -1 在邊界,0,不在,1在

// 點在凸多邊形內的判定, 判斷點是不是在所有邊的左邊

int inpoly( point p, point * poly, int n )

if( wn )

return 1;

return 0;

}// 凸包

int tb( point *p, int n, point *ch )

int k = m;

for( int i = n - 2; i >= 0; --i )

if( n > 1 )

m--;

return m;

}// 點在有向線段左邊

bool onleft( line l, point p )

// 二直線交點,假設交點唯一存在

point getintersection( line a, line b )

// 半平面交

int halfins( line *l, int n, point *poly )

if( first < last )

p[last-1] = getintersection( q[last-1], q[last] );

} while( first < last && !onleft( q[first], p[last-1] ) )

last--;

if( last - first <= 1 )

return 0;

p[last] = getintersection( q[last], q[first] );

int m = 0;

for( int i = first; i <= last; ++i )

poly[m++] = p[i];

return m;

} // */

計算幾何 二維幾何 模板

幾何意義 向量的點積 a b a b a b cos cos 若cos cos 為正,兩向量之間的夾角為銳角 為負,兩向量夾角為鈍角 為量,兩向量夾角為直角。b在a方向上的投影 向量的叉積 a b a b a b sin sin 數值上表示a和b構成的平行四邊形的面積。include using n...

計算幾何 圓 二維模板

include include include include include define fir first define sec second using namespace std const double eps 1e 8 const double pi acos 1.0 int dcmp...

二維計算幾何模板(點,線)

include include include include include include include include include include include define e exp 1.0 define mod 1000000007 define inf 0x7fffffff d...