1 计算两个二维向量的夹角
#include <iostream>
#include <cmath>
struct PoseInfo {
float x;
float y;
};
typedef PoseInfo Point2D;
typedef PoseInfo Vector2D;
float Vector2DAngle(const Vector2D& vec1, const Vector2D& vec2)
{
double PI = 3.141592653;
float t = (vec1.x * vec2.x + vec1.y * vec2.y) / (sqrt(pow(vec1.x, 2) + pow(vec1.y, 2)) * sqrt(pow(vec2.x, 2) + pow(vec2.y, 2)));
float angle = acos(t) * (180 / PI);
return angle;
}
int main()
{
Point2D vecA_start_point;
vecA_start_point.x = 0.0;
vecA_start_point.y = 0.0;
Point2D vecA_end_point;
vecA_end_point.x = 1.0;
vecA_end_point.y = 0.0;
Point2D vecB_end_point;
vecB_end_point.x = 1.0;
vecB_end_point.y = 5.0;
Vector2D vecA;
vecA.x = vecA_end_point.x - vecA_start_point.x;
vecA.y = vecA_end_point.y - vecA_start_point.y;
Vector2D vecB;
vecB.x = vecB_end_point.x - vecA_start_point.x;
vecB.y = vecB_end_point.y - vecA_start_point.y;
float angle = Vector2DAngle(vecA, vecB);
std::cout << "angle = " << angle << std::endl;
getchar();
return 0;
}
计算结果:
angle = 78.6901
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:计算几何 – C++计算两个二维向量的夹角
原文链接:https://www.stubbornhuang.com/1559/
发布于:2021年08月12日 14:24:45
修改于:2023年06月26日 21:21:41
当前分类随机文章推荐
- C++ - 常用的C++命令行参数解析第三方库 阅读4243次,点赞3次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读1382次,点赞0次
- C++ - queue存储动态指针时正确释放内存 阅读6182次,点赞2次
- C++ - 日志库easylogging++初始化时不生成默认日志文件mylog.txt 阅读84次,点赞0次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读5044次,点赞0次
- C++ - websocket++库的可使用的所有事件总结 阅读424次,点赞0次
- C++ - const_cast, static_cast, dynamic_cast, reinterpret_cast四种cast转换的区别和使用 阅读102次,点赞0次
- C++ - 单例模式 阅读83次,点赞0次
- C++读取Shp文件并将Shp转化为DXF 阅读3509次,点赞1次
- C++ - return this和return *this的含义和区别 阅读652次,点赞0次
全站随机文章推荐
- TensorRT - Using an engine plan file across different models of devices is not recommended and is likely to affect performance or even cause errors 阅读584次,点赞0次
- 资源分享 - TCP/IP网络编程(韩 尹圣雨著 金国哲译)PDF下载 阅读7898次,点赞2次
- 资源分享 - 3D游戏与计算机图形学中的数学方法 第3版 , Mathematics for 3D Game Programming and Computer Graphics, Third Edition 中文版PDF下载 阅读1937次,点赞0次
- 默认的左手坐标系与右手坐标系的比较 阅读4607次,点赞2次
- Pytorch - 模型微调时删除原有模型中的某一层的方法 阅读3230次,点赞0次
- Pytorch - 检测CUDA、cuDNN以及GPU版本的Pytorch是否安装成功、GPU显存测试 阅读4760次,点赞1次
- Python - itertools.groupby使用详解 阅读1480次,点赞1次
- WordPress - 站点底部显示站点运行时间 阅读4966次,点赞2次
- 资源分享 - Visualizing Quaternions 英文高清PDF下载 阅读2146次,点赞0次
- 资源分享 - Advanced High Dynamic Range Imaging, First Edition 英文高清PDF下载 阅读1850次,点赞0次
评论
169