• 求空间两向量夹角


    空间三维向量的叉乘:

     

    向量的点乘:

     因此结合(0)和(1)可以的得到: 

                      θ = atan2(sin(θ),cos(θ)) = atan2((A×B)n,AB) = atan2((A×B).norm(),AB)      { 0<θ<180 }

    #include <iostream>
    #include <Eigen/Dense>
    
    typedef Eigen::Vector3d Point;
    
    double getDegAngle(Point p1, Point p2, Point p3) {
        Eigen::Vector3d v1 = p2 - p1;
        Eigen::Vector3d v2 = p3 - p1;
        //one method, radian_angle belong to 0~pi
        //double radian_angle = atan2(v1.cross(v2).transpose() * (v1.cross(v2) / v1.cross(v2).norm()), v1.transpose() * v2);
        //another method, radian_angle belong to 0~2pi
        double radian_angle = atan2(v1.cross(v2).norm(), v1.transpose() * v2);
        if (v1.cross(v2).z() < 0) {
            radian_angle = 2 * M_PI - radian_angle;
        }
        return radian_angle * 180 / M_PI;
    }
    
    int main() {
        //Point p1(0, 0, 0), p2(1, 0, 0), p3(0, -1, 0);
        //Point p1(0, 0, 0), p2(1, 0, 0), p3(0, 1, 0);
        //Point p1(0, 0, 0), p2(1, 0, 0), p3(0.5, 0.5, 0);
        Point p1(0, 0, 0), p2(1, 0, 0), p3(0.5, -0.5, 0);
        std::cout << "The Degree Angle is: " << getDegAngle(p1, p2, p3) << "
    ";
        return 0;
    }
            //求两个矢量的夹角
            inline double getAngleTwoVectors(const Eigen::Vector3d & v1, const Eigen::Vector3d & v2) {
                double radian_angle = atan2(v1.cross(v2).norm(), v1.transpose() * v2);
                return radian_angle;   //[0,PI]
            }
            inline double getAngleTwoVectorsPlane(const Eigen::Vector3d & v1, const Eigen::Vector3d & v2) {
                double dot2 = v1[0]*v2[0]+v1[1]*v2[1];
                double dis1 = sqrt(v1[0] * v1[0] + v1[1] * v1[1]);
                double dis2 = sqrt(v2[0] * v2[0] + v2[1] * v2[1]);
                double radian_angle = acos(dot2/(dis1*dis2));
                return radian_angle;   //[0,PI]
            }
  • 相关阅读:
    《快速软件开发》学习笔记 之一
    Python+常用模块(2).md
    Python语法 (1).md
    使用mysql导入txt文件
    Python+numpy(3).md
    笔试二(程序题)
    啦啦啦 我的博客开通了
    java面试笔试
    笔试三(面试二)
    笔试三(面试)
  • 原文地址:https://www.cnblogs.com/lovebay/p/11411512.html
Copyright © 2020-2023  润新知