计算几何板子(待更新)
Posted yzhang-rp-inf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算几何板子(待更新)相关的知识,希望对你有一定的参考价值。
***计算几何好难啊orz
背板子就行
背会后复制黏贴也是珂以的
我在这里写就是方便大家复制黏贴的
//计算几何模板
//made by yzhang
//基础向量定义
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Vector A,Vector B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double p)
{
return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,double p)
{
return Vector(A.x/p,A.y/p);
}
bool operator <(const Point &a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-10;
inline int dcmp(register double x)
{
if(fabs(x)<eps)
return 0;
else
return x<0?-1:1;
}
bool operator == (const Point &a,const Point &b)
{
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
//点积,向量长度,夹角
inline double Dot(register Vector A,register Vector B)
{
return A.x*B.x+A.y*B.y;
}
inline double Length(register Vector A)
{
return sqrt(Dot(A,A));
}
inline double Angle(register Vector A,register Vector B)
{
return acos(Dot(A,B)/Length(A)/Length(B));
}
//叉积
inline double Cross(register Vector A,register Vector B)
{
return A.x*B.y-A.y*B.x;
}
inline double Area2(register Vector A,register Vector B)
{
return Cross(B-A,C-A);
}
//向量旋转
inline Vector Rotate(register Vector A,register double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
//单位法线(雾)
inline Vector Normal(register Vector A)
{
double L=Length(A);
return Vector(-A.y/L,A.x/L);
}
//另一种写法(慢的很orz)(complex就在complex库中)
typedef complex<double> Point;
typedef Point Vector;
inline double Dot(register Vector A,register Vector B)
{
return real(conj(A)*B);
}
inline double Cross(register Vector A,register Vector B)
{
return imag(conj(A)*B);
}
inline Vector Rotate(register Vector A,register double rad)
{
return A*exp(Point(0,rad));
}
//直线交点
inline Point Getlineintersection(register Point P,register Vector v,register Point Q,register Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
//点到直线的距离(有向距离就把fabs去了)
inline double Distancetoline(register Point P,register Point A,register Point B)
{
Vector v1=B-A,v2=P-A;
return fabs(Cross(v1,v2))/Length(v1);
}
//点到线段的距离
inline double(register Point P,register Point A,register Point B)
{
if(A==B)
return Length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if(dcmp(Dot(v1,v2))<0)
return Length(v2);
else if(dcmp(Dot(v1,v3))>0)
return Length(v3);
else
return fabs(Cross(v1,v2))/Length(v1);
}
//点在直线上的投影
inline Point Getlineprojection(register Point P,register Point A,register Point B)
{
Vector v=B-A;
return A+v*(Dot(v,P-A)/Dot(v,v));
}
//线段相交-无端点
inline bool SegmentProperintersection(register Point a1,register Point a2,register Point b1,register Point b2)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
//有端点
inline bool Onsegment(register Point p,register Point a1,register Point a2)
{
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
//计算多边形的有向面积
inline double PolygonArea(register Point *p,register int n)
{
double area=0;
for(register int i=1;i<n-1;++i)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
以上是关于计算几何板子(待更新)的主要内容,如果未能解决你的问题,请参考以下文章