示例#1
0
        public static float DistanceTo(Vector2 line1, Vector2 line2, Vector2 a)
        {
            float r = Math.Min(Length(line1, a), Length(line2, a));

            float length = LineMath.Length(line1, line2);

            if (length == 0)
            {
                return(r);
            }

            Vector2 n  = LineMath.Scale(LineMath.Sub(line1, line2), 1.0f / length);
            float   d1 = LineMath.Mult(line1, n) - LineMath.Mult(a, n);

            n = new Vector2(-n.Y, n.X);
            float d2 = LineMath.Mult(line1, n) - LineMath.Mult(a, n);

            if (d1 <= length && d1 >= 0)
            {
                return(Math.Min(Math.Abs(d2), r));
            }
            return(r);
        }