1.2.3 compile an interval2d use case to accept parameters n, Min, and Max from the command line. Generates n random 2D intervals, with the width and height evenly distributed between min and Max in the unit square. Use stddraw to plot them and print the number of corresponding interval pairs and the number of contained interval pairs.
Solution: intersection refers to the situation where two rectangular areas have common points, including a rectangle inside a rectangle, and also contains an intersection. Two rectangles intersect and the three vertices of a rectangle are located in the area of another rectangle.
Add the following code to the interval2d. Java file to obtain the coordinates in the upper left, lower left, and upper right corner of 2D.
Public point2d lefttopcorner (){
Return new point2d (X. Min (), Y. Min ());
}
Public point2d leftbottom (){
Return new point2d (X. Min (), Y. Max ());
}
Public point2d righttopcorner (){
Return new point2d (X. Max (), Y. Min ());
}
The following code is used to meet the requirements of this question.
Public class Test2
{
Public static void main (string [] ARGs)
{
Int n = integer. parseint (ARGs [0]);
Double min = double. parsedouble (ARGs [1]);
Double max = double. parsedouble (ARGs [2]);
Interval2d [] inter2darray = new interval2d [N];
//
Double lo;
Double Hi;
// Generate interval2d
For (INT I = 0; I <n; I ++)
{
Lo = stdrandom. Uniform (Min, max );
Hi = stdrandom. Uniform (Lo, max );
Interval1d x = new interval1d (Lo, hi );
//
Lo = stdrandom. Uniform (Min, max );
Hi = stdrandom. Uniform (Lo, max );
Interval1d y = new interval1d (Lo, hi );
//
Inter2darray [I] = new interval2d (x, y );
Inter2darray [I]. Draw ();
}
// Find Intersect and contains
Int intersectcount = 0;
Int containcount = 0;
For (INT I = 0; I <n; I ++)
For (Int J = I + 1; j <n; j ++)
{
// Intersect
If (inter2darray [I]. intersects (inter2darray [J])
Intersectcount ++;
// Contains: intersect and contains three top point
If (
(Inter2darray [I]. Contains (inter2darray [J]. lefttopcorner ())&&
Inter2darray [I]. Contains (inter2darray [J]. leftbottom ())&&
Inter2darray [I]. Contains (inter2darray [J]. righttopcorner ())
) |
(Inter2darray [J]. Contains (inter2darray [I]. lefttopcorner ())&&
Inter2darray [J]. Contains (inter2darray [I]. leftbottom ())&&
Inter2darray [J]. Contains (inter2darray [I]. righttopcorner ())
)
)
Containcount ++;
}
Stdout. printf ("intersectcount = % d, containcount = % d", intersectcount, containcount );
} // End main
} // End class
Algs4-1.2.3 to write an interval2d use case