參照學(xué)校大佬的模板整理出文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-622228.html
定義
const double eps=1e-18;
//記錄點(diǎn)的橫縱坐標(biāo)
struct Pot{
double x,y;
};
//記錄線段的起始點(diǎn)和終點(diǎn)
struct Line{
Pot st,ed;
};
//記錄圓的圓心和半徑
struct Circle{
Pot p;
double r;
};
判斷小數(shù)大小和正負(fù)
//判斷小數(shù)的正負(fù),如果絕對(duì)值小于精度就返回0,小于0就返回-1,否則返回1
int sign(double x){
if(fabs(x)<eps)return 0;
if(x<0)return -1;
return 1;
}
//比較小數(shù)的大小,如果x-y小于精度那么認(rèn)為他們相等,如果x<y就返回-1,x大于y就返回1
int dcmp(double x,double y){
if(fabs(x-y)<eps)return 0;
if(x<y)return -1;
return 1;
}
兩個(gè)點(diǎn)的坐標(biāo)加減乘除運(yùn)算
Pot operator + (Pot a,Pot b){
return {(a.x+b.x),(a.y +b.y )};
}
//兩個(gè)點(diǎn)相減的坐標(biāo)
Pot operator - (Pot a,Pot b){
return {(a.x -b.x ),(a.y -b.y )};
}
//判斷兩個(gè)點(diǎn)是否坐標(biāo)相等
bool operator == (Pot a,Pot b){
if(dcmp(a.x,b.x)==0&&dcmp(a.y,b.y)==0)return 1;
return 0;
}
兩個(gè)向量運(yùn)算
//求一個(gè)向量長(zhǎng)度乘c之后的向量
Pot operator * (Pot a,double c){
return {(a.x*c),(a.y *c)};
}
//求一個(gè)向量除以c之后的向量
Pot operator / (Pot a,double c){
return {(a.x/c),(a.y /c)};
}
//向量a在向量b上的投影與b長(zhǎng)度的乘積
//a*b=|a||b|cos(c)
double dot(Pot a,Pot b){
return a.x*b.x+a.y *b.y ;
}
//向量a與b張成的平行四邊形的有向面積,b在a的逆時(shí)針?lè)较驗(yàn)檎?/span>
//a*b=|a||b|sin(c)
double cross(Pot a,Pot b){
return a.x *b.y -b.x *a.y ;
}
點(diǎn)到點(diǎn)的距離
//a到原點(diǎn)的距離
double get_length(Pot a){
return sqrt(dot(a,a));
}
//點(diǎn)a和點(diǎn)b之間的距離
double dist(Pot a,Pot b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
點(diǎn)到線的距離
//點(diǎn)p到直線ab的距離
double dist_to_line(Pot p,Pot a,Pot b){
Pot v1=b-a,v2=p-a;
return fabs(cross(v1,v2)/get_length(v1));
}
//點(diǎn)p到線段ab的距離
double dist_to_seg(Pot p,Pot a,Pot b){
if(a==b)return get_length(p-a);
Pot v1=b-a,v2=p-a,v3=p-b;
if(sign(dot(v1,v2))<0)return get_length(v2);
if(sign(dot(v1,v3))>0)return get_length(v3);
return dist_to_line(p,a,b);
}
兩直線交點(diǎn),判斷線段是否相交
//求兩直線的交點(diǎn),v為p的向量,w為q的向量
Pot get_Line_inter(Pot p,Pot v,Pot q,Pot w){
Pot u=p-q;
double t=cross(w,u)/cross(v,w);
return p+v*t;
}
//判斷兩線段是否相交
bool xiangjiao(Pot a1,Pot a2,Pot b1,Pot b2){
double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1);
double c3=cross(b2-b1,a2-b1),c4=cross(b2-b1,a1-b1);
return sign(c1)*sign(c2)<=0&&sign(c3)*sign(c4)<=0;
}
返回點(diǎn)aob形成的角的角度
//返回角aOb的角度,返回的是弧度
double get_angle(Pot a,Pot b){
return acos(dot(a,b)/get_length(a)/get_length(b));
}
已知三點(diǎn)坐標(biāo)求形成的三角形面積
//返回三個(gè)點(diǎn)構(gòu)成的三角形面積的兩倍
double area(Pot a,Pot b,Pot c){
return cross(b-a,c-a);
}
已知三條邊長(zhǎng)求三角形面積
//三條邊長(zhǎng)求三角形面積
double helen(double a,double b,double c){
double p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
一個(gè)點(diǎn)繞原點(diǎn)順時(shí)針旋轉(zhuǎn)x度
//返回三個(gè)點(diǎn)構(gòu)成的三角形面積的兩倍
double area(Pot a,Pot b,Pot c){
return cross(b-a,c-a);
}
判斷點(diǎn)是否在線段上
//判斷點(diǎn)p是否在線段ab上
bool on_seg(Pot p,Pot a,Pot b){
return sign(cross(p-a,p-b))==0&&sign(dot(p-a,p-b))<=0;
}
n個(gè)點(diǎn)組成的多邊形的面積
//n個(gè)點(diǎn)從0到n-1求多邊形面積
double Polygon_area(Pot p[],int n){
double s=0;
for(int i=1;i+1<n;i++){
s+=cross(p[i]-p[0],p[i+1]-p[i]);
}
return s/2;
}
點(diǎn)在直線上的投影
//點(diǎn)p在直線ab上的投影
Pot get_line_pro(Pot p,Pot a,Pot b){
Pot v=b-a;
return a+v*(dot(v,p-a)/dot(v,v));
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-622228.html
到了這里,關(guān)于計(jì)算幾何公式(點(diǎn)到直線距離,點(diǎn)到線段距離等)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!