Beispiel #1
0
    public static bool SegIntersectPlg(ref VInt2 segSrc, ref VInt2 segVec, VInt2[] plg, out VInt2 nearPoint, out VInt2 projectVec)
    {
        nearPoint  = VInt2.zero;
        projectVec = VInt2.zero;
        if (plg == null || plg.Length < 2)
        {
            return(false);
        }
        bool result = false;
        long num    = -1L;
        int  num2   = -1;

        for (int i = 0; i < plg.Length; i++)
        {
            VInt2 vInt = plg[(i + 1) % plg.Length] - plg[i];
            VInt2 vInt2;
            if (IntMath.IntersectSegment(ref segSrc, ref segVec, ref plg[i], ref vInt, out vInt2))
            {
                long sqrMagnitudeLong = (vInt2 - segSrc).sqrMagnitudeLong;
                if (num < 0L || sqrMagnitudeLong < num)
                {
                    nearPoint = vInt2;
                    num       = sqrMagnitudeLong;
                    num2      = i;
                    result    = true;
                }
            }
        }
        if (num2 >= 0)
        {
            VInt2 lhs   = plg[(num2 + 1) % plg.Length] - plg[num2];
            VInt2 vInt3 = segSrc + segVec - nearPoint;
            long  num3  = (long)vInt3.x * (long)lhs.x + (long)vInt3.y * (long)lhs.y;
            if (num3 < 0L)
            {
                num3 = -num3;
                lhs  = -lhs;
            }
            long sqrMagnitudeLong2 = lhs.sqrMagnitudeLong;
            projectVec.x = (int)IntMath.Divide((long)lhs.x * num3, sqrMagnitudeLong2);
            projectVec.y = (int)IntMath.Divide((long)lhs.y * num3, sqrMagnitudeLong2);
        }
        return(result);
    }