C++ 实现两个向量之间的夹角

C++ 实现两个向量之间的夹角具体原理可以参考另外一篇博客 点击打开链接 实现思想就是 通过计算两个向量的斜率角 然后相减 就得到了夹角 好了 直接上代码 include include include includeusing usingnamespa 以 pt1 为基准 floatgetAnge Point2f amp pt1 P

        具体原理可以参考另外一篇博客:点击打开链接,实现思想就是,通过计算两个向量的斜率角,然后相减,就得到了夹角,好了,直接上代码!

 

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/177526.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午7:18
下一篇 2026年3月26日 下午7:18


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号