1.2.3编写一个Interval2D用例,从命令行接受参数N、min和max。生成N个随机的2D间隔,其宽和高均匀地分布在单位正方形中的min和max之间。用StdDraw画出它们并打印出相关的间隔对的数量以及有包含关系的间隔对数量。
解:相交是两个矩形区域有共点,包含是指一个矩形在一个矩形内,包含也是相交的一种情况。两个矩形相交并且一个矩形的三个顶点在另一个矩形区域内,那么两个矩形有包含关系。
将下面的代码添加到Interval2D.java文件中,用来获取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());
}
下面的代码用来实现本题要求
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