/// <summary> /// Searches for the two closests points /// </summary> /// <param name="intersectionPoints">List of intersection points</param> /// <returns>The points that are closests to each other</returns> public static ClosestPointPair ShortestDistanceBetweenCutPoints(List <Point> intersectionPoints) { List <ClosestPointPair> Distances = new List <ClosestPointPair>(); ClosestPointPair shortestClosest = new ClosestPointPair(); ClosestPointPair closest = new ClosestPointPair(); shortestClosest.distance = 100000; for (int i = 0; i < intersectionPoints.Count - 1; i++) { for (int j = i + 1; j < intersectionPoints.Count; j++) { closest.point1 = intersectionPoints[i]; closest.point2 = intersectionPoints[j]; closest.distance = Math.Pow((Math.Pow((intersectionPoints[i].x - intersectionPoints[j].x), 2) + Math.Pow((intersectionPoints[i].y - intersectionPoints[j].y), 2)), 0.5); Distances.Add(closest); } } foreach (ClosestPointPair dist in Distances) { if (dist.distance < shortestClosest.distance) { shortestClosest.distance = dist.distance; shortestClosest.point1 = dist.point1; shortestClosest.point2 = dist.point2; } } return(shortestClosest); }
/// <summary> /// Calculates the centroid of the cluster /// </summary> /// <param name="crossings">The obtained cutting points</param> /// <returns></returns> private static Point Cluster(List <Point> crossings, int anchors) { List <Point> cluster = new List <Point>(); Point centroid = new Point(); //first pass. ClosestPointPair closestPointPair = new ClosestPointPair(); //get the two closest intersection point closestPointPair = GeometryHelper.ShortestDistanceBetweenCutPoints(crossings); // get struct back //add these points to the cluster cluster.Add(new Point(closestPointPair.point1.x, closestPointPair.point1.y)); cluster.Add(new Point(closestPointPair.point2.x, closestPointPair.point2.y)); //remove them from the intersection crossings.Remove(closestPointPair.point1); crossings.Remove(closestPointPair.point2); centroid.x = (cluster[0].x + cluster[1].x) / 2; centroid.y = (cluster[0].y + cluster[1].y) / 2; crossings.Add(centroid); while (cluster.Count < anchors && crossings.Count > 0) { Point closestPoint = GeometryHelper.ShortestDistanceBetweenCutPoints(crossings, centroid); cluster.Add(closestPoint); crossings.Remove(closestPoint); Point tempCentroid = new Point(); foreach (Point clusterPoint in cluster) { tempCentroid.x += clusterPoint.x; tempCentroid.y += clusterPoint.y; } tempCentroid.x /= cluster.Count; tempCentroid.y /= cluster.Count; centroid = tempCentroid; } return(centroid); }