判断一个点是否在一个区域内


/// 
/// 判断一个点是否在一个区域内
/// 
public class PolyUtil
{
    //notice, in the case of point[0] == point[1] == point[2] == point[3]
    //this function return false positive...
    public static bool Contains(Vector3[] point, Vector3 p)
    {
        var dp0 = p - point[0];
        var dp1 = p - point[1];
        var dp2 = p - point[2];
        var dp3 = p - point[3];

        var cdp01 = Vector3.Cross(dp0, dp1);
        var cdp12 = Vector3.Cross(dp1, dp2);
        var cdp23 = Vector3.Cross(dp2, dp3);
        var cdp30 = Vector3.Cross(dp3, dp0);

        return Vector3.Dot(cdp01, cdp12) >= 0 &&
               Vector3.Dot(cdp12, cdp23) >= 0 &&
               Vector3.Dot(cdp23, cdp30) >= 0;

    }
}

你可能感兴趣的:(判断一个点是否在一个区域内)