Accord.Math.Geometry.Line.DistanceToPoint C# (CSharp) Method

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
        public float DistanceToPoint(Point point)
        {
            float distance;

            if (!IsVertical)
            {
                float div = (float)Math.Sqrt(k * k + 1);
                distance = Math.Abs((k * point.X + b - point.Y) / div);
            }
            else
            {
                distance = Math.Abs(b - point.X);
            }

            return distance;
        }

Usage Example

        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));
        }
All Usage Examples Of Accord.Math.Geometry.Line::DistanceToPoint