DistanceToPoint() public method

Calculate Euclidean distance between a point and a line.
public DistanceToPoint ( Point point ) : float
point Point The point to calculate distance to.
return float
        private static ConvexityDefect extractDefect(List <IntPoint> contour, int startIndex, int endIndex)
        {
            // Navigate the contour until the next point of the convex hull,
            //  taking note of the distance between the current contour point
            //  and the line connecting the two consecutive convex hull points

            IntPoint start = contour[startIndex];
            IntPoint end   = contour[endIndex];
            Line     line  = Line.FromPoints(start, end);

            double maxDepth = 0;
            int    maxIndex = 0;

            if (startIndex < endIndex)
            {
                for (int i = startIndex; i < endIndex; i++)
                {
                    double d = line.DistanceToPoint(contour[i]);

                    if (d > maxDepth)
                    {
                        maxDepth = d;
                        maxIndex = i;
                    }
                }
            }
            else
            {
                for (int i = startIndex; i < contour.Count; i++)
                {
                    double d = line.DistanceToPoint(contour[i]);

                    if (d > maxDepth)
                    {
                        maxDepth = d;
                        maxIndex = i;
                    }
                }

                for (int i = 0; i < endIndex; i++)
                {
                    double d = line.DistanceToPoint(contour[i]);

                    if (d > maxDepth)
                    {
                        maxDepth = d;
                        maxIndex = i;
                    }
                }
            }

            return(new ConvexityDefect(maxIndex, startIndex, endIndex, maxDepth));
        }
Example #2
0
        /// <summary>
        /// Calculate Euclidean distance between a point and a finite line segment.
        /// </summary>
        ///
        /// <param name="point">The point to calculate the distance to.</param>
        ///
        /// <returns>Returns the Euclidean distance between this line segment and the specified point. Unlike
        /// <see cref="Line.DistanceToPoint"/>, this returns the distance from the finite segment. (0,0) is 5 units
        /// from the segment (0,5)-(0,8), but is 0 units from the line through those points.</returns>
        ///
        public float DistanceToPoint(Point point)
        {
            float segmentDistance;

            switch (LocateProjection(point))
            {
            case ProjectionLocation.RayA:
                segmentDistance = point.DistanceTo(start);
                break;

            case ProjectionLocation.RayB:
                segmentDistance = point.DistanceTo(end);
                break;

            default:
                segmentDistance = line.DistanceToPoint(point);
                break;
            }
            ;

            return(segmentDistance);
        }