Algs4-1.2.3 to write an interval2d use case

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.