計算幾何模板

2022-08-19 18:24:09 字數 4869 閱讀 9854

我在演算法競賽從入門到高階那裡看的,通過做題還加了一些函式,覺得不錯來看看吧!(有新的函式演算法我會持續更新放入,記得來看!)

#include

#include

using namespace std;

#define ll long long

#define n 100005

#define eps 0.00000001//偏差值1e8

#define pi acos(-1.0)//高精度圓周率

const int maxp = 1010; //點的數量

int sgn(double x)

int dcmp(double x, double y)

//——————————————————平面幾何:點和線————————————————————————

struct point

point(double x, double y) :x(x), y(y) {}

point operator + (point b)

point operator - (point b)

point operator * (double k) //長度增大k倍

point operator / (double k) //長度縮小k倍

bool operator ==(point b)

bool operator <(point b) //比較兩個點,用於凸包計算

};typedef point vector; //定義向量

double dot(vector a, vector b) //點積

double len(vector a) //向量的長度

double len2(vector a) //向量長度的平方

//a與b的夾角

double angle(vector a, vector b)

double cross(vector a, vector b) //叉積

//三角形abc面積的兩倍

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

//兩點的距離,用兩種方式實現

double distance(point a, point b)

double dist(point a, point b)

//向量a的單位法向量

vector normal(vector a)

//向量是否平行或重合

bool parallel(vector a, vector b)

vector rotate(vector a, double rad)

struct line

line(point p1,point p2):p1(p1),p2(p2){}

//根據乙個點和傾斜角angle確定直線,0≤anglebool getcross(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double &tx,double &ty)//求兩直線交點

//——————————————平面幾何:多邊形————————————————————————

struct polygon

;//判斷點和任意多邊形的關係:3為點上;2為邊上;1為內部;0為外部

int point_in_polygon(point pt, point* p, int n)

for (i = 0; i < n; i++)

int num = 0;

for (int i = 0; i < n; i++)

return num != 0;

}//多邊形面積

double polygon_area(point* p, int n)

double mult(point a, point b, point o)

int graham(point p, int n, point res)

int len = top;

res[++top] = p[n - 2];

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

return top;

}//求多邊形的重心

point polygon_center(point* p, int n)

//convex_hull()求凸包.凸包頂點放在ch中,返回值是凸包的頂點數

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

int j = v;

//求上凸包

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

if (n > 1)v--;

return v; //返回值v是凸包的頂點數

}//——————————————平面幾何:圓——————————————

struct circle

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

circle(double x, double y, double _r)

};//點和圓的關係:0為點在圓內,1為點在圓上,2為點在圓外

int point_circle_relation(point p, circle c)

//直線和圓的關係:0為直線在圓內,1為直線和圓相切,2為直線在圓外

int line_circle_relation(line v, circle c)

//線段和圓的關係:0為線段在圓內,1為線段和圓相切,2為線段在圓外

int seg_circle_relation(segment v, circle c)

//直線和圓的交點.pa、pb是交點.返回值是交點的個數

int line_cross_circle(line v, circle c, point& pa, point& pb)

point n = (v.p2 - v.p1) / len(v.p2 - v.p1); //單位向量

pa = q + n * k;

pb = q - n * k;

return 2; //兩個交點

}//————————————————三維幾何————————————————————————————

//三維:點

struct point3

point3(double x, double y, double z) :x(x), y(y), z(z){}

point3 operator + (point3 b)

point3 operator - (point3 b)

point3 operator * (double k)

point3 operator / (double k)

bool operator ==(point3 b)

};typedef point3 vector3;

//點積.和二維點積函式同名.c++允許函式同名

double dot(vector3 a, vector3 b)

//叉積

vector3 cross(vector3 a, vector3 b)

double len(vector3 a) //向量的長度

double len2(vector3 a) //向量的長度的平方

double distance(point3 a, point3 b)

//a與b的夾角

double angle(vector3 a, vector3 b)

//三維:線

struct line3

line3(point3 p1,point3 p2):p1(p1),p2(p2){}

};typedef line3 segment3; //定義線段,兩端點是point p1,p2

//三維:三角線面積的兩倍

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

//三維:點到直線的距離

double dis_point_line(point3 p, line3 v)

//三維:點在直線上

bool point_line_relation(point3 p, line3 v)

//三維:點到線段的距離

double dis_point_seg(point3 p, segment3 v)

//三維:點p在直線上的投影

point3 point_line_proj(point3 p, line3 v)

//三維:平面

struct plane

plane(point3 p1,point3 p2,point3 p3):p1(p1),p2(p2),p3(p3){}

};//平面法向量

point3 pvec(point3 a, point3 b, point3 c)

point3 pvec(plane f)

//四點共平面

bool point_on_plane(point3 a, point3 b, point3 c, point3 d)

//兩平面平行

int parallel(plane f1, plane f2)

//兩平面垂直

int vertical(plane f1, plane f2)

//直線與平面的交點p,返回值是交點的個數

int line_cross_plane(line3 u, plane f, point3& p)

//四面體有向體積x6

double volume4(point3 a, point3 b, point3 c, point3 d)

計算幾何模板

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

計算幾何模板

多圓面積交 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...