Eigen库学习笔记(1) 距离和夹角

求一点到原点的距离, Pt(x,y)

Vector2d v1(x,y);
double  res1= v1.norm();              //   等于 sqrt(x^2+y^2) , 即距离
double  res2 = v1.squaredNorm();     //    (x^2+y^2)

求2点之间距离  pt1(x1,y1) , pt2(x2,y2)

	CPoint pt1(10, 10), pt2(5, 5);
	Vector2d v4(pt1.x, pt1.y), v5(pt2.x, pt2.y);
	double  len = (v4 - v5).norm();

求三点形成的夹角, ptSrc为出发点.  参阅 判断两个向量之间夹角是逆时针或顺时针 ,还需补充方向判断

	Vector2d v1(pt1.x-ptSrc.x , pt1.y - ptSrc.y ), v2( pt2.x-ptSrc.x, pt2.y-ptSrc.y );
	double cosValNew = v1.dot(v2) / (v1.norm()*v2.norm());
	double angleNew = acos(cosValNew) * 180 / M_PI;

或者用 atan, 但eigen的cross必须是三维向量, 必须把二维先转三维, 有点不好

Vector3d v1(pt1.x - ptSrc.x, pt1.y - ptSrc.y ,0 ), v2(pt2.x - ptSrc.x, pt2.y - ptSrc.y, 0);
double tanVal = v1.cross(v2).norm()/(v1.dot(v2));
double  angleNew =(  atan(tanVal)) * 180 / M_PI;

总和(sum()),乘积(prod())最大值(maxCoeff())最小值(minCoeff()),trace() 主对角线和,mean()平均值

 

#include 
#include 
using namespace std;
int main()
{
  Eigen::Matrix2d mat;
  mat << 1, 2,
         3, 4;
  cout << "Here is mat.sum():       " << mat.sum()       << endl;
  cout << "Here is mat.prod():      " << mat.prod()      << endl;
  cout << "Here is mat.mean():      " << mat.mean()      << endl;
  cout << "Here is mat.minCoeff():  " << mat.minCoeff()  << endl;
  cout << "Here is mat.maxCoeff():  " << mat.maxCoeff()  << endl;
  cout << "Here is mat.trace():     " << mat.trace()     << endl;
}

Here is mat.sum():       10
Here is mat.prod():      24
Here is mat.mean():      2.5
Here is mat.minCoeff():  1
Here is mat.maxCoeff():  4
Here is mat.trace():     5

参考教程 : Eigen教程

 

你可能感兴趣的:(vc,Eigen)