計算幾何的簡單模板

2021-09-06 10:52:37 字數 3003 閱讀 1575

const double eps = 1e-8;

struct point

};typedef point vtor;

//向量的加減乘除

vtor operator + (vtor a,vtor b)

vtor operator - (point a,point b)

vtor operator * (vtor a,double p)

vtor operator / (vtor a,double p)

bool operator < (point a,point b)

int dcmp(double x)

bool operator == (point a,point b)

//向量的點積,長度,夾角

double dot(vtor a,vtor b)

double length(vtor a)

double angle(vtor a,vtor b)

//叉積,三角形面積

double cross(vtor a,vtor b)

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

//向量的旋轉,求向量的單位法線(即左轉90度,然後長度歸一)

vtor rotate(vtor a,double rad)

vtor normal(vtor a)

//直線的交點

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

//點到直線的距離

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

//點到線段的距離

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

//點到直線的對映

point getlineprojection(point p,point a,point b)

//判斷線段是否規範相交

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

//判斷點是否在一條線段上

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

//多邊形面積

double polgonarea(point *p,int n)

和圓有關的計算

struct line

point point(double t)

};struct circle

point point(double a)

};//判斷圓與直線是否相交以及求出交點

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

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(vtor v)

int getcircleintersection(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*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 gettangents(point p,circle c,vtor *v)

else

}//求兩圓的切線

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

//圓心距的平方

double 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);

double rdiff = a.r - b.r;

double rsum = a.r + b.r;

double base = angle(b.c - a.c);

//重合有無限多條

if (d2 == 0 && dcmp(a.r - b.r) == 0) return -1;

//內切

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

//有外公切線

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 (dcmp(d2 - rsum*rsum) == 0)

//兩條內切線

else if (dcmp(d2 - rsum*rsum) > 0)

return cnt;

}

計算幾何學簡單的模板

一些定義 include include include include include using namespace std define maxn 1200 define eps 1e 8 struct point po maxn struct line typedef point vecto...

計算幾何模板

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 ...

計算幾何模板

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...