C++ 实现两个向量之间的夹角
2017-07-15 09:29
1531 查看
具体原理可以参考另外一篇博客:点击打开链接,实现思想就是,通过计算两个向量的斜率角,然后相减,就得到了夹角,好了,直接上代码!
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
// 以pt1为基准
float getAngelOfTwoVector(Point2f &pt1, Point2f &pt2, Point2f &c)
{
float theta = atan2(pt1.x - c.x, pt1.y - c.y) - atan2(pt2.x - c.x, pt2.y - c.y);
if (theta > CV_PI)
theta -= 2 * CV_PI;
if (theta < -CV_PI)
theta += 2 * CV_PI;
theta = theta * 180.0 / CV_PI;
return theta;
}
void main()
{
Point2f c(0, 0);
Point2f pt1(0, -1);
Point2f pt2(-1, 0);
float theta = getAngelOfTwoVector(pt1, pt2, c);
cout << "theta: " << theta << endl;
}
打完收工!
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
// 以pt1为基准
float getAngelOfTwoVector(Point2f &pt1, Point2f &pt2, Point2f &c)
{
float theta = atan2(pt1.x - c.x, pt1.y - c.y) - atan2(pt2.x - c.x, pt2.y - c.y);
if (theta > CV_PI)
theta -= 2 * CV_PI;
if (theta < -CV_PI)
theta += 2 * CV_PI;
theta = theta * 180.0 / CV_PI;
return theta;
}
void main()
{
Point2f c(0, 0);
Point2f pt1(0, -1);
Point2f pt2(-1, 0);
float theta = getAngelOfTwoVector(pt1, pt2, c);
cout << "theta: " << theta << endl;
}
打完收工!
相关文章推荐
- C++实现两个字符串之间的Levenshtein Distance(编辑距离)
- 用Python计算两个向量之间的夹角余弦值
- 判断两个向量之间夹角是逆时针或顺时针
- 通过矩阵求两个向量集中元素两两之间的欧氏距离(python实现)
- 通过矩阵求两个向量集中元素两两之间的欧氏距离(python实现)
- 通过矩阵求两个向量集中元素两两之间的欧氏距离(python实现),比直接分开求更快速
- 【c/c++】完成端口服务器中转实现两个客户端之间通信
- 分享:用Python计算两个向量之间的夹角余弦值
- 使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动
- JS使用正则截取两个字符串之间的字符串实现方法详解
- 用两个函数来实现求1到某个数字之间所有的素数并输出
- 串行通信总结(实现两个单片机之间的通信)(很好的例子,跟风进步)
- C++实现两个多项式相加
- 求两个整数的最小公倍数和最大公约数的算法及其C++实现
- 两个无线路由器之间实现无线连接
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- 两个链表之间的联合----Java实现
- Java实现求两个日期之间相差的天数
- C++ 一个exe的两个运行实例之间共享数据
- 计算两个日期之间的天数差C++/java