Example #1
0
        // ---------------------------------------------------------------
        // Determine shortest distance to ring perimeter
        // i0 and i1 are indices for vertexes around closest line in ring
        // In the case of equality, one is chosen specifically to handle
        //   the degenerate case in point-in-polygon test
        // ---------------------------------------------------------------
        public float DistanceToRingPerimeter(Vector3 v, out int i0, out int i1)
        {
            int prevIndex = verts.Count - 1;

            i0 = prevIndex;
            i1 = 0;
            float dMin         = float.PositiveInfinity;
            float lastLineDist = VectorUtil.DistanceToLine(v, verts[prevIndex - 1], verts[prevIndex]);

            for (int i = 0; i < verts.Count; i++)
            {
                float lineDist = VectorUtil.DistanceToLine(v, verts[prevIndex], verts[i]);;
                float d        = VectorUtil.DistanceToEdge(v, verts[prevIndex], verts[i]);
                if (d == dMin)   // handle degenerate case for point-in-polygon test
                {
                    if (lineDist > lastLineDist)
                    {
                        dMin = d;
                        i0   = prevIndex;
                        i1   = i;
                    }
                }
                else if (d < dMin)
                {
                    dMin = d;
                    i0   = prevIndex;
                    i1   = i;
                }
                lastLineDist = lineDist;
                prevIndex    = i;
            }
            return(dMin);
        }