来一波全套向量运算(C++)

时间:2023-03-09 19:29:18
来一波全套向量运算(C++)
//头文件要求
#include <cmath> struct P{long long x, y;}p[N]; //加法
P operator +(P x, P y){return (P){x.x + y.x, x.y + y.y};} //减法
P operator -(P x, P y){return (P){x.x - y.x, x.y - y.y};} // 乘法
P operator *(P x, P y){return (P){x.x * y.x - x.y * y.y, x.y * y.x + x.x * y.y};} //叉积
long long cross(P x, P y){return x.y * y.x - x.x * y.y;} //数量积 点积
long long dot(P x, P y){return x.x * y.x + x.y * y.y;} //四舍五入除法
long long dv(long long a, long long b){//注意重名!!!
return b < 0 ? dv(-a, -b)
: (a < 0 ? -dv(-a, b)
: (a + b / 2) / b);} //模长平方
long long len(P x){return x.x * x.x + x.y * x.y;} //模长
long long dis(P x){return sqrt(x.x * x.x + x.y * x.y);} //向量除法
P operator /(P x, P y){
long long l = len(y);
return (P){dv(dot(x, y), l), dv(cross(x, y), l)};
} //向量膜
P operator %(P x, P y){return x - ((x / y) * y);} //向量GCD
P gcd(P x, P y){return len(y) ? gcd(y, x % y) : x;}