/** * Unit tests the {@code GrahamScan} data type. * Reads in an integer {@code n} and {@code n} points (specified by * their <em>x</em>- and <em>y</em>-coordinates) from standard input; * computes their convex hull; and prints out the points on the * convex hull to standard output. * * @param args the command-line arguments */ public static void main(String[] args) { 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); for (Point2D p : graham.hull()) StdOut.println(p); }
public FarthestPair(Point2D[] pdarr) { this.bestDistance = double.NegativeInfinity; GrahamScan grahamScan = new GrahamScan(pdarr); if (pdarr.Length <= 1) { return; } int num = 0; Iterator iterator = grahamScan.hull().iterator(); while (iterator.hasNext()) { Point2D arg_46_0 = (Point2D)iterator.next(); num++; } Point2D[] array = new Point2D[num + 1]; int num2 = 1; Iterator iterator2 = grahamScan.hull().iterator(); while (iterator2.hasNext()) { Point2D point2D = (Point2D)iterator2.next(); Point2D[] arg_8A_0 = array; int arg_8A_1 = num2; num2++; arg_8A_0[arg_8A_1] = point2D; } if (num == 1) { return; } if (num == 2) { this.best1 = array[1]; this.best2 = array[2]; this.bestDistance = this.best1.distanceTo(this.best2); return; } int num3 = 2; while (Point2D.area2(array[num], array[num3 + 1], array[1]) > Point2D.area2(array[num], array[num3], array[1])) { num3++; } int num4 = num3; for (int i = 1; i <= num3; i++) { if (array[i].distanceTo(array[num4]) > this.bestDistance) { this.best1 = array[i]; this.best2 = array[num4]; this.bestDistance = array[i].distanceTo(array[num4]); } while (num4 < num && Point2D.area2(array[i], array[num4 + 1], array[i + 1]) > Point2D.area2(array[i], array[num4], array[i + 1])) { num4++; double num5 = array[i].distanceTo(array[num4]); if (num5 > this.bestDistance) { this.best1 = array[i]; this.best2 = array[num4]; this.bestDistance = array[i].distanceTo(array[num4]); } } } }
/** * Computes the farthest pair of points in the specified array of points. * * @param points the array of points * @throws IllegalArgumentException if {@code points} is {@code null} or if any * entry in {@code points[]} is {@code null} */ public FarthestPair(Point2D[] points) { if (points == null) throw new IllegalArgumentException("constructor argument is null"); for (int i = 0; i < points.length; i++) { if (points[i] == null) throw new IllegalArgumentException("array element " + i + " is null"); } GrahamScan graham = new GrahamScan(points); // single point if (points.length <= 1) return; // number of points on the hull int m = 0; for (Point2D p : graham.hull()) m++; // the hull, in counterclockwise order hull[1] to hull[m] Point2D[] hull = new Point2D[m+1]; m = 1; for (Point2D p : graham.hull()) { hull[m++] = p; } m--; // 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[1], hull[k+1]) > Point2D.area2(hull[m], hull[1], hull[k])) { k++; } int j = k; for (int i = 1; i <= k && j <= m; i++) { // StdOut.println("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[i+1], hull[j+1]) > Point2D.area2(hull[i], hull[i+1], hull[j])) { j++; // StdOut.println(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]); } } } }