二維計算幾何
宣告:由於本人較弱,並不能保證以下內容的100%正確
歡迎大佬來挑錯
基本定義
1點積struct
point
6 point(double xx,double
yy)9
};10
typedef point vec;
1112 vec operator +(vec v1,vec v2)
15 vec operator -(point p1,point p2)
18 vec operator * (vec v,double
k)21 vec operator / (vec v,double
k)24
25bool
operator
< (const point &a,const point &b)
2930
double myabs(double
x)34
35const
double eps=1e-10;36
int dcmp(double
x)41
42bool
operator == (const point &a,const point &b)
1叉積double
dt(vec v1,vec v2)
4double
getlen(vec v)
7double
getang(vec v1,vec v2)
1基本運算double
crs(vec v1,vec v2)
4double
gets2(point a,point b,point c)
二維計算幾何常用演算法
點在多邊形內的判定
1二維凸包int ispointinpolygon(point p,vectorpoly)
12if(wn!=0)return1;
13else
return0;
14 }
注意輸入不能有重複點
精度高時使用dcmp比較
1旋轉卡殼求直徑int convexhull(point *p,int n,point *ch)
8int k=m;
9for(int i=n-2;i>=0;--i)
13if(n>1)m--;
14return
m;15 }
1半平面交double rotatingcaliper(point* ch,int
m)10
return
ans;
11 }
o(n2)
半平面交注意判斷無解和無界
1 typedef vectorpolygon;o(nlogn)2polygon cutpolygon(polygon poly,point a,point b)13}
14return
newpoly;
15 }
1bool
onleft(line l,point p)
4point getlineintersection(line a,line b)910
point p[maxn];
11line q[maxn];
12int halfplaneintersection(line *l,int n,point *poly)
25if(h1]=getlineintersection(q[t-1
],q[t]);26}
27while((h1])))--t;
28if(t-h<=1)return0;
29 p[t]=getlineintersection(q[t],q[h]);
3031
int m=0;32
for(int i=h;i<=t;++i)poly[m++]=p[i];
33return
m;34 }
bool onleft(line l,point p)point getlineintersection(line a,line b)
point p[maxn];
line q[maxn];
int halfplaneintersection(line *l,int n,point *poly){
sort(l,l+n);
int h,t;
q[h=t=1]=l[0];
for(int i=1;i
計算幾何 二維幾何 模板
幾何意義 向量的點積 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 include include include include using namespace std 二維幾何基礎 struct point typedef point vector const doub...
計算幾何 圓 二維模板
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...