#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
#define Point Vector
const double EPS = 1e-10;
struct Vector {
double x, y;
Vector(double x = 0, double y = 0) : x(x), y(y) {}
Vector operator +(Vector B) {
return Vector(x + B.x, y + B.y);
}
Vector operator -(Vector B) {
return Vector(x - B.x, y - B.y);
}
Vector operator *(double k) {
return Vector(k * x, k * y);
}
Vector operator /(double k) {
return Vector(x / k, y / k);
}
bool operator ==(Vector B) {
if(fabs(x - B.x) < EPS && fabs(y - B.y) < EPS) return true;
else return false;
}
double dot(Vector B) {
return x * B.x + y * B.y; //两向量的点积
}
double length() {
return sqrt(x * x + y * y);
}
double cosAngleOfTwoVectors(Vector B) {
return dot(B) / length() / B.length(); //两向量夹角的余弦值
}
double valueOfDet(Vector B) {
return x * B.y - y * B.x; //两向量叉积的大小
}
Vector rotate(double rad) { //逆时针旋转rad弧度
double s = sin(rad), c = cos(rad);
return Vector(x * c - y * s, x * s + y * c);
}
Vector unitNormalVector() {
double L = length(); //要保证A不是零向量
return Vector(-y / L, x / L); //求单位法向量
}
};
//typedef Vector Point;
#endif // VECTOR_H_INCLUDED
Vector.h
最新推荐文章于 2025-08-09 13:49:22 发布