算法代码小片段

    /*
     *  求两点夹角
     */
    public static float GetAngleTwoPoint(float px1, float py1, float px2, float py2)
    {
        float x1 = px2 - px1;
        float y1 = py2 - py1;
        float hypotenuse = Mathf.Sqrt(Mathf.Pow(x1, 2) + Mathf.Pow(y1, 2));
        float cos = x1 / hypotenuse;
        float radian = Mathf.Acos(cos);
        float angle = 180.0f / (Mathf.PI / radian);
        if (y1 < 0)
        {
            angle = -angle;
        }
        else if ((y1 == 0) && (x1 < 0))
        {
            angle = 180.0f;
        }
        return angle;
    }

 

posted @ 2015-08-21 17:05  泥潭里的金鱼  阅读(146)  评论(0)    收藏  举报