求向量夹角

向量夹角公式:

  cosα = A*B / (|A|*|B|)

  设A(x1,y1),B(x2,y2);

  cosα =(x1*x2 + y1*y2) / sqrt( (x1^2 + y1^2) * (x2^2 + y2^2) )

代码实现(c++):

#include <math.h>

//Calcucate angle between vector A and B
//返回角度; return degree
int CalculateVectorAngle(int x1, int y1, int x2, int y2)
{
//acos return radian,we should transform it into degree
return acos((x1*x2 + y1*y2) / sqrt((x1*x1 + y1*y1)*(x2*x2 + y2*y2)) * 180 / 3.14;
}
posted @ 2011-05-27 23:33  LittleAnt  阅读(10214)  评论(2编辑  收藏  举报