Unity 计算圆的切点

    /// <summary>
    /// 计算过圆外一点的切点,在圆上的切点
    /// </summary>
    /// <param name="center"></param>
    /// <param name="radius"></param>
    /// <param name="point"></param>
    /// <param name="tanPoint1"></param>
    /// <param name="tanPoint2"></param>
    /// <returns></returns>
    public static bool GetTangentPoint(Vector3 center, float radius, Vector3 point, out Vector3 tanPoint1, out Vector3 tanPoint2)
    {
        float dis = Vector3.Distance(center, point);
        if (dis <= 0 || radius < 0 || dis < radius)
        {
            //点必须在圆的外部
            tanPoint1 = point;
            tanPoint2 = point;
            return false;
        }
        float acos = Mathf.Acos(radius / dis) * Mathf.Rad2Deg;//根据余弦求出角度
        Vector3 dir = point - center;

        //绕着Y轴旋转向量,模长是半径,得到的点就是切点
        tanPoint1 = (Quaternion.Euler(0, acos, 0) * dir).normalized * radius + center;
        tanPoint2 = (Quaternion.Euler(0, -acos, 0) * dir).normalized * radius + center;
        return true;
    }

 

posted on 2023-11-11 18:59  Jason_c  阅读(78)  评论(0编辑  收藏  举报