C++ 实现两个向量之间的夹角
生活随笔
收集整理的這篇文章主要介紹了
C++ 实现两个向量之间的夹角
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? 具體原理可以參考另外一篇博客:點擊打開鏈接,實現思想就是,通過計算兩個向量的斜率角,然后相減,就得到了夾角,好了,直接上代碼!
?
#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.y - c.y, pt1.x - c.x) - atan2(pt2.y - c.y, pt2.x - c.x);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++ 实现两个向量之间的夹角的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java插入排序(思路及实现)
- 下一篇: s3c2440移植MQTT