public static void MainTest(string[] args) { TextInput StdIn = new TextInput(); int N = StdIn.ReadInt(); Point2D[] points = new Point2D[N]; for (int i = 0; i < N; i++) { int x = StdIn.ReadInt(); int y = StdIn.ReadInt(); points[i] = new Point2D(x, y); } GrahamScan graham = new GrahamScan(points); foreach (Point2D p in graham.Hull()) { Console.WriteLine(p); } }
/// <summary> /// Computes the farthest pair of points in the specified array of points.</summary> /// <param name="points">the array of points</param> /// <exception cref="NullReferenceException">if <c>points</c> is <c>null</c> or if any /// entry in <c>points[]</c> is <c>null</c></exception> /// public FarthestPair(Point2D[] points) { GrahamScan graham = new GrahamScan(points); // single point if (points.Length <= 1) { return; } // number of points on the hull int M = 0; foreach (Point2D p in graham.Hull()) { M++; } // the hull, in counterclockwise order Point2D[] hull = new Point2D[M + 1]; int m = 1; foreach (Point2D p in graham.Hull()) { hull[m++] = p; } // all points are equal if (M == 1) { return; } // points are collinear if (M == 2) { best1 = hull[1]; best2 = hull[2]; bestDistanceSquared = best1.DistanceSquaredTo(best2); return; } // k = farthest vertex from edge from hull[1] to hull[M] int k = 2; while (Point2D.Area2(hull[M], hull[k + 1], hull[1]) > Point2D.Area2(hull[M], hull[k], hull[1])) { k++; if (k == hull.Length - 1) { break; } } int j = k; for (int i = 1; i <= k; i++) { //Console.WriteLine(hull[i] + " and " + hull[j] + " are antipodal"); if (hull[i].DistanceSquaredTo(hull[j]) > bestDistanceSquared) { best1 = hull[i]; best2 = hull[j]; bestDistanceSquared = hull[i].DistanceSquaredTo(hull[j]); } while ((j < M) && Point2D.Area2(hull[i], hull[j + 1], hull[i + 1]) > Point2D.Area2(hull[i], hull[j], hull[i + 1])) { j++; //Console.WriteLine(hull[i] + " and " + hull[j] + " are antipodal"); double distanceSquared = hull[i].DistanceSquaredTo(hull[j]); if (distanceSquared > bestDistanceSquared) { best1 = hull[i]; best2 = hull[j]; bestDistanceSquared = hull[i].DistanceSquaredTo(hull[j]); } } } }