Esempio n. 1
0
        /// <summary>
        /// Gets the nearest line segment index from the given point.
        /// </summary>
        /// <returns>The smaller index of the segment vertex, or -1 if the point is too far from the segments.</returns>
        protected static int GetSegmentIndex(IList <Point> vertices, Point point, double maxDistance = 5)
        {
            if (vertices.Count < 2)
            {
                return(-1);
            }
            var nearestIndex = -1;
            var nearestDist  = double.PositiveInfinity;

            for (int i = 0, j = vertices.Count - 1; i < j; i++)
            {
                if (!CanvasHelper.IsBetweenPoints(point, vertices[i], vertices[i + 1]))
                {
                    continue;
                }
                var dist = CanvasHelper.DistanceToLine(point, vertices[i], vertices[i + 1]);
                if (dist > maxDistance)
                {
                    continue;
                }
                if (dist < nearestDist)
                {
                    nearestIndex = i;
                    nearestDist  = dist;
                }
            }
            return(nearestIndex);
        }