求给定三个点的夹角

求给定三个点的夹角,其实是求$$<\vec{A},\vec{B}>$$两个向量之间的夹角$$\alpha$$。本文采用如下公式求解。

$$\cos{\alpha}=\frac{{A}\times{B}}{\|A\|\|B\|}$$

源代码如下:

double getAngleByThreeP(double pointx[3], double pointy[3])
{
    double a_b_x = pointx[0] - pointx[1];
    double a_b_y = pointy[0] - pointy[1];
    double c_b_x = pointx[2] - pointx[1];
    double c_b_y = pointy[2] - pointy[1];
    double ab_mul_cb = a_b_x * c_b_x + a_b_y * c_b_y;
    double dist_ab = sqrt(a_b_x * a_b_x + a_b_y * a_b_y);
    double dist_cd = sqrt(c_b_x * c_b_x + c_b_y * c_b_y);
    double cosValue = ab_mul_cb / (dist_ab * dist_cd);
    return acos(cosValue);
}

 

posted @ 2014-04-22 19:34  搬砖程序员带你飞  阅读(718)  评论(0编辑  收藏  举报