求给定三个点的夹角,其实是求<A ⃗ ,B ⃗ >两个向量之间的夹角α 。本文采用如下公式求解。
cosα=A×B/∥A∥∥B∥
源代码(Java)如下:
private double getAngleByThreeP(double[] pointx, double[] pointy) { 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 = Math.sqrt(a_b_x * a_b_x + a_b_y * a_b_y); double dist_cd = Math.sqrt(c_b_x * c_b_x + c_b_y * c_b_y); double cosValue = ab_mul_cb / (dist_ab * dist_cd); return Math.acos(cosValue); }
求出来的结果是弧度值(另:角度值=弧度*180/π)
转载自【搬砖程序员】