点的定义
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
向量的定义
typedef Point Vector;
运算
加减乘除
Point operator + (Point A,Point B){
return Point(A.x+B.x,A.y+B.y);
}
Point operator - (Point A,Point B){
return Point(A.x-B.x,A.y-B.y);
}
Point operator * (Point A,double p){
return Point(A.x*p,A.y*p);
}
Point operator / (Point A,double p){
return Point(A.x/p,A.y/p);
}
小于
首先按照 x x x 坐标升序排序,再按照 y y y坐标升序排序
bool operator < (const Point& a,const Point& b){
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;
}
等于
bool operator == (const Point& a,const Point& b){
if(fabs(a.x-b.x)<eps && fabs(a.y-b.y)<eps) return true;
return false;
}
内积
α
⋅
β
=
∣
α
∣
∣
β
∣
c
o
s
θ
\alpha ·\beta=|\alpha||\beta|cos\theta
α⋅β=∣α∣∣β∣cosθ
几何意义为向量
α
\alpha
α在向量
β
\beta
β下的投影与
β
\beta
β长度的乘积
double Dot(Vector A,Vector B){
return A.x*B.x+A.y*B.y;
}
外积
α
×
β
=
∣
α
∣
∣
β
∣
s
i
n
θ
\alpha \times \beta=|\alpha||\beta|sin\theta
α×β=∣α∣∣β∣sinθ
几何意义为以向量
α
\alpha
α和向量
β
\beta
β为邻边构成的平行四边形的面积
double Cross(Point A,Point B){
return A.x*B.y-A.y*B.x;
}
常用函数
取模(向量长度)
double Length(Vector A){
return sqrt(Dot(A,A));
}
两个向量弧度制下的夹角
double Rad(Point A,Point B){
return fabs(atan2(fabs(Cross(a,b)),Dot(a,b)));
}
向量A绕着P点逆时针旋转rad角度之后的向量
Point Rotate(Point A,Point P,double angle){
Point v=A-P;
double c=cos(angle),s=sin(angle);
return Point(p.x+v.x*c-v.y*s,p.y+v.x*s+v.y*c);
}