計算幾何模板

2021-07-23 11:27:27 字數 2281 閱讀 5388

#include

#define vct point

using

namespace

std;

const

double pi=atan2(0,-1);

const

double eps=1e-8;

int sgn(double d)

struct point

bool

operator==(point b)const

//基本運算

vct operator+(vct b);}

vct operator-(vct b);}

vct operator*(double d);}

vct operator/(double d);}

double dot(vct b)

double cross(vct b)

double dis()

double dis(point b)

double agl()

double agl(vct b)

vct rot(double d);}

//線段

if(*this==a||*this==b)return

0; vct pa=a-*this,pb=b-*this;

return sgn(pa.cross(pb))==0&&pa.dot(pb)<0?1:-1;

}double dis(point a,point b)

//多邊形

template

int cmp(t first,t last)

return wn!=0?1:-1;

}};struct line//方向向量必須單位化,小心除0錯誤

point getp(double t)

point intersection(line l)

double dis(point q)//不取絕對值就是有向距離

point projection(point q)

};//線段

bool intersect(point a,point b,point c,point d)

double agl(double a,double b,double c)//角c

struct circle*r;}

int intersection(line l,vector

&ans)

double t=sqrt(r*r-d*d);

ans.push_back(q+l.v*t);

ans.push_back(q-l.v*t);

return

2;//相交

}int intersection(circle c,vector

&ans)

if(sgn(r+c.r-d)<0)return

0;//外離

if(sgn(fabs(r-c.r)-d)>0)return

0;//內含

double a=(o-c.o).agl();//o1o2極角

double b=agl(r,c.r,d);

point p1=getp(a+b),p2=getp(a-b);

ans.push_back(p1);

if(p1==p2)return

1;//相切

ans.push_back(p2);

return

2;//相交

}int tangent(point p,vector

&ans)

double a=asin(d/r);

ans.push_back(line(p,po.rot(a)));

ans.push_back(line(p,po.rot(-a)));

return

2; }

};int tangent(circle a,circle b,vector

&va,vector

&vb)

//有外公切線

double ang=acos((rsub)/d);

va.push_back(a.getp(base+ang));

vb.push_back(b.getp(base+ang));

va.push_back(a.getp(base-ang));

vb.push_back(b.getp(base-ang));

if(sgn(d-rsum)==0)

if(sgn(d-rsum)>0)

return

2;//相交

}

計算幾何模板

sgn返回x經過eps處理的符號,負數返回 1,正數返回1,x的絕對值如果足夠小,就返回0。const double eps 1e 8 int sgn double x double mysqrt double x pt是point的縮寫 int版 struct pt pt int x,int y ...

計算幾何模板

多圓面積交 typedef long long ll typedef unsigned long long ull typedef vector vi const int inf 0x3f3f3f3f const double eps 1e 10 const int mod 100000007 co...

計算幾何 模板

include include include include typedef double db const db eps 1e 9 const db pi acos 1 判斷符號 inline intsign db ps 比較大小 inline intcmp db ps,db b 點 struc...