您的位置:首页 > 编程语言 > C语言/C++

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;
}


打完收工!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: