title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
算法4 Java解答 1.2.03 |
2019-02-22 07:35:21 +0800 |
false |
|
|
Write an Interval2D client that takes command-line arguments N, min, and max and generates N random 2D intervals whose width and height are uniformly distributed between min and max in the unit square. Draw them on StdDraw and print the number of pairs of intervals that intersect and the number of intervals that are contained in one another.
for (int i = 0; i < N; i++) {
double x1 = StdRandom.uniform(min, max);
double x2 = StdRandom.uniform(x1, max);
double y1 = StdRandom.uniform(min, max);
double y2 = StdRandom.uniform(y1, max);
StdOut.printf("%.3f %.3f %.3f %.3f\n", x1, x2, y1, y2);
interval2DS[i] = new Interval2D(new Interval1D(x1, x2), new Interval1D(y1, y2));
interval2DS[i].draw();
}
// input : 5 1 3
// 2.086 2.868 2.681 2.888
// 1.317 1.796 1.334 1.685
// 1.076 1.169 1.161 2.466
// 2.718 2.898 1.879 2.822
// 1.124 2.785 2.056 2.757
// intersects: [2.085732774635417, 2.867627605042185] x [2.6812764408234657, 2.888416623450271] [2.717900071730835, 2.898131211101078] x [1.8787075417069883, 2.8223422183654305]
// intersects: [2.085732774635417, 2.867627605042185] x [2.6812764408234657, 2.888416623450271] [1.1244512150893864, 2.7848273473246246] x [2.0561356585718746, 2.7572245972031375]
// intersects: [1.076212020768767, 1.1691905904168232] x [1.1610568511465638, 2.466455232585124] [1.1244512150893864, 2.7848273473246246] x [2.0561356585718746, 2.7572245972031375]
// intersects: [2.717900071730835, 2.898131211101078] x [1.8787075417069883, 2.8223422183654305] [1.1244512150893864, 2.7848273473246246] x [2.0561356585718746, 2.7572245972031375]