示例#1
0
        /// <summary>
        /// Calculates the minimum distance between a point and a line.
        /// </summary>
        /// <param name="p">A point.</param>
        /// <param name="origin">Line origin point.</param>
        /// <param name="dir">Line direction.</param>
        /// <returns>The minimum distance between the point and the line.</returns>
        public static double PointLineDistance(Vector2 p, Vector2 origin, Vector2 dir)
        {
            double  t               = Vector2.DotProduct(dir, p - origin);
            Vector2 pPrime          = origin + t * dir;
            Vector2 vec             = p - pPrime;
            double  distanceSquared = Vector2.DotProduct(vec, vec);

            return(Math.Sqrt(distanceSquared));
        }
示例#2
0
        /// <summary>
        /// Checks if a point is inside a line segment.
        /// </summary>
        /// <param name="p">A point.</param>
        /// <param name="start">Segment start point.</param>
        /// <param name="end">Segment end point.</param>
        /// <returns>Zero if the point is inside the segment, 1 if the point is after the end point, and -1 if the point is before the start point.</returns>
        public static int PointInSegment(Vector2 p, Vector2 start, Vector2 end)
        {
            Vector2 dir    = end - start;
            Vector2 pPrime = p - start;
            double  t      = Vector2.DotProduct(dir, pPrime);

            if (t <= 0)
            {
                return(-1);
            }
            double dot = Vector2.DotProduct(dir, dir);

            if (t >= dot)
            {
                return(1);
            }
            return(0);
        }