Cup Blue Bridge Group B @java basic training exercises (30) BASIC-018: Rectangular cross area
Key words: the line segment AC is determined
Problem Description
With two rectangular plane, their edges parallel to the X-axis or Y-axis of the rectangular coordinate system. For each rectangle, we give it the coordinates of a pair of opposite vertices, you calculate the area of programming post two rectangles.
Input format
input containing only two lines, with each line describing a rectangle.
In each row, are given opposite vertices of a pair of rectangular coordinates, with the coordinates of each point are two real absolute value is not more than 10 ^ 7 FIG.
Output format
output includes only a real number, is the area of the post, retained to two decimal places.
Sample input
. 1. 1. 3. 3
2 2. 4. 4
Sample Output
1.00
Code:
public class BASIC018判断¥线段交 {
public static void main(String[] args) {
java.util.Scanner s=new java.util.Scanner(System.in);//录入四个点的坐标值
double x1=s.nextDouble();double y1=s.nextDouble();
double x2=s.nextDouble();double y2=s.nextDouble();
double x3=s.nextDouble();double y3=s.nextDouble();
double x4=s.nextDouble();double y4=s.nextDouble();
s.close();
//分别把两个矩形的坐标点中的最大值和最小值单独取出来(为判断两矩形的位置关系做备)
//第一个矩形中给定的两个点中的x坐标中的最大和最小
double maxx1=Math.max(x1, x2),minx1=Math.min(x1, x2);
//第一个矩形中给定的两个点中的y坐标中的最大和最小
double maxy1=Math.max(y1, y2),miny1=Math.min(y1, y2);
//第二个矩形中给定的两个点中的x坐标中的最大和最小
double maxx2=Math.max(x3, x4),minx2=Math.min(x3, x4);
//第二个矩形中给定的两个点中的y坐标中的最大和最小
double maxy2=Math.max(y3, y4),miny2=Math.min(y3, y4);
//通过判断两个矩形的位置关系来选择不同的结果(或计算方式);
if(maxx1<=minx2||maxy1<=miny2||minx1>=maxx2||miny1>=maxy2){
System.out.println("0.00"); //当两个矩形不想交的时候,就没有相交图形产生
}else{//若相交,求出所成矩形的两条边长再相乘就是结果
double a1=Math.max(minx1,minx2),b1=Math.max(miny1,miny2);
double a2=Math.min(maxx1, maxx2),b2=Math.min(maxy1, maxy2);
System.out.println(String.format("%.2f",(Math.abs(a1-a2)*Math.abs(b1-b2))));
}
}
}