DistancePointLine() public static method

Computes the distance from a point p to a line segment AB. Note: NON-ROBUST!
public static DistancePointLine ( System.Point p, System.Point a, System.Point b ) : double
p System.Point The point to compute the distance for.
a System.Point One point of the line.
b System.Point Another point of the line (must be different to A).
return double
Beispiel #1
0
        public static double DistanceToLine(Point point, List <Point> points)
        {
            var minDist = Double.MaxValue;

            for (var i = 0; i < points.Count - 1; i++)
            {
                var dist = CGAlgorithms.DistancePointLine(point, points[i], points[i + 1]);
                if (dist < minDist)
                {
                    minDist = dist;
                }
            }

            return(minDist);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the shortest distance to a line and also the index of the segment
        /// with that shortest distance. Segments count from zero to vertex count - 1.
        /// </summary>
        /// <param name="point"></param>
        /// <param name="points"></param>
        /// <returns></returns>
        public static (double Distance, int Segment) GetDistanceAndSegmentIndex(Point point, IList <Point> points)
        {
            var minDist = Double.MaxValue;
            int segment = 0;

            for (var i = 0; i < points.Count - 1; i++)
            {
                var dist = CGAlgorithms.DistancePointLine(point, points[i], points[i + 1]);
                if (dist < minDist)
                {
                    minDist = dist;
                    segment = i;
                }
            }

            return(minDist, segment);
        }