自主开发,计算几何常用二维向量类 vector_2d
支持的东西:
向量加减,数乘,点积,叉积,夹角计算,垂直、平行判断,向量旋转,模长计算。
注意如果要使用判断功能要先设定 eps 精度值,或者直接外置 eps 常数。
还有就是重载类型选浮点型,要不然会直接上天。
欢迎各位读者提供改进意见(正确性修正,常数优化,精度优化等),您的意见会被记录在此。
第一版。
参考资料:OI-wiki 向量,感谢!
template<class Tp>//float or double or long double
class vector_2d{
public: Tp x,y;
public: Tp eps;
public: Tp myabs(Tp t){
return (t>0.0)?(t):(-t);
}
public: void seteps(Tp neps){//精度设置
eps=neps;
}
public: void build(Tp tx,Tp ty){
x=tx;y=ty;
}
public: Tp length(){//模长计算
return sqrt(x*x+y*y);
}
public: void rotate(Tp theta){//+theta,逆时针旋转
vector_2d<Tp>res;
res.build(x*cos(theta)-y*sin(theta),y*cos(theta)+x*sin(theta));
x=res.x;y=res.y;
}
public: void multiply(Tp lamda){//数乘
x*=lamda;y*=lamda;
}
public: bool parallel(vector_2d tp2){//平行判断
return myabs(((*this)^tp2))<eps;
}
public: bool perpendicular(vector_2d tp2){//垂直判断
return myabs((*this)*tp2)<eps;
}
public: Tp angle(vector_2d tp2){//求夹角
return acos((*this)*tp2/(*this).length()/tp2.length());
}
friend vector_2d operator + (vector_2d tp1,vector_2d tp2){
vector_2d<Tp>res;
res.build(tp1.x+tp2.x,tp1.y+tp2.y);
return res;
}
friend vector_2d operator - (vector_2d tp1,vector_2d tp2){
vector_2d<Tp>res;
res.build(tp1.x-tp2.x,tp1.y-tp2.y);
return res;
}
friend Tp operator * (vector_2d tp1,vector_2d tp2){//点积
return tp1.x*tp2.x+tp1.y*tp2.y;
}
friend Tp operator ^ (vector_2d tp1,vector_2d tp2){//叉积
return tp1.x*tp2.y-tp2.x*tp1.y;
}
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?