/// <summary> /// Return the point index or -1 if not a vertex of the polygon /// </summary> /// <param name="polygon"></param> /// <param name="position"></param> /// <returns></returns> public static int FindPoint(this Polygon polygon, IntPoint position, INearestNeighbours <int> nearestNeighbours = null) { if (nearestNeighbours != null) { var index = nearestNeighbours.GetNearestNeighbour(position); if (position == polygon[index]) { return(index); } } else { for (int i = 0; i < polygon.Count; i++) { if (position == polygon[i]) { return(i); } } } return(-1); }
public static int FindClosestPositionIndex(this Polygon polygon, IntPoint position, INearestNeighbours <int> nearestNeighbours = null) { if (nearestNeighbours != null) { return(nearestNeighbours.GetNearestNeighbour(position)); } else { int bestPointIndex = -1; double closestDist = double.MaxValue; for (int pointIndex = 0; pointIndex < polygon.Count; pointIndex++) { double dist = (polygon[pointIndex] - position).LengthSquared(); if (dist < closestDist) { bestPointIndex = pointIndex; closestDist = dist; } } return(bestPointIndex); } }