/// <summary> /// Check if there is a closer distance that arround the divider /// </summary> /// <param name="pointsSortedByY">The points list which sorted by y-coodinate already</param> /// <param name="middleX">The x-coodinate of the middle line which split the points into two parts</param> /// <param name="closestPair">The closest pair between left part and right part</param> /// <returns> /// a pair points which distance closer than minDistanse /// return null if could not find any /// </returns> static PairOfPoints FindCloserPairFromDividerArea(List<Point> pointsSortedByY, double middleX, PairOfPoints closestPair) { // find all the points within minDistanse of line middleX. var pointsInDividerArea = pointsSortedByY.Where(p => Math.Abs(middleX - p.X) <= closestPair.Distance); // Check all points sorted by y-coodinate for (int i = 0; i < pointsSortedByY.Count - 1; i++) { // need to check only the 7 points for (int j = i + 1; j < i + 8 && j < pointsSortedByY.Count; j++) { // Check if this pair of points closer than minDistanse if (pointsSortedByY[i].Distance(pointsSortedByY[j]) < closestPair.Distance) { closestPair = new PairOfPoints(pointsSortedByY[i], pointsSortedByY[j]); } } } return closestPair; }
/// <summary> /// Check if there is a closer distance that arround the divider /// </summary> /// <param name="pointsSortedByY">The points list which sorted by y-coodinate already</param> /// <param name="middleX">The x-coodinate of the middle line which split the points into two parts</param> /// <param name="closestPair">The closest pair between left part and right part</param> /// <returns> /// a pair points which distance closer than minDistanse /// return null if could not find any /// </returns> static PairOfPoints FindCloserPairFromDividerArea(List <Point> pointsSortedByY, double middleX, PairOfPoints closestPair) { // find all the points within minDistanse of line middleX. var pointsInDividerArea = pointsSortedByY.Where(p => Math.Abs(middleX - p.X) <= closestPair.Distance); // Check all points sorted by y-coodinate for (int i = 0; i < pointsSortedByY.Count - 1; i++) { // need to check only the 7 points for (int j = i + 1; j < i + 8 && j < pointsSortedByY.Count; j++) { // Check if this pair of points closer than minDistanse if (pointsSortedByY[i].Distance(pointsSortedByY[j]) < closestPair.Distance) { closestPair = new PairOfPoints(pointsSortedByY[i], pointsSortedByY[j]); } } } return(closestPair); }