Eigen:向量之间的夹角和距离

  1. 求一点到原点的距离
Vector3d v(x,y,z);
double dis1= v.norm();              // 等于 sqrt(x^2+y^2) , 即距离
double dis2 = v.squaredNorm();     // (x^2+y^2)
  1. 求两点之间的距离
d v1(x1,y1,z1);
Vector3d v2(x2,y2,z2);
double dis = (v1-v2).norm();
  1. 两个向量之间的夹角
Vector3d v1(x1,y1,z1), v2(x2,y2,z2);
double cosValNew = v1.dot(v2) / (v1.norm()*v2.norm()); //角度cos值
double angleNew = acos(cosValNew) * 180 / M_PI;       //弧度角

测试:

    Eigen::Vector3f a(1,2,3);
    Eigen::Vector3f b(2,3,4);  

    std::cout << "a.norm() = " << a.norm() << std::endl;
    std::cout << "a.squaredNorm() = " << a.squaredNorm() << std::endl;
    std::cout << "(a-b).norm() = " << (a-b).norm() << std::endl;
    double cosValNew = a.dot(b) / (a.norm()*b.norm()); //角度cos值
    double angleNew = acos(cosValNew) * 180 / M_PI;       //弧度角
    std::cout << "cosValNew = " << cosValNew << std::endl;
    std::cout << "angleNew = " << angleNew << std::endl;

a.norm() = 3.74166
a.squaredNorm() = 14
(a-b).norm() = 1.73205
cosValNew = 0.992583
angleNew = 6.98253

上一篇:STM32个人学习记录(1)


下一篇:【Python|密码学】仿射加密法实验报告