Rectangles
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 11631 Accepted Submission (s): 3716
Problem Description
Given two rectangles and the coordinates of two points on the diagonals of each rectangle, you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY.
Input
Input The first line of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal. the 8 numbers are x1, y1, x2, y2, x3, y3, x4, y4.That means the two points on the first rectangle are (x1, y1), (x2, y2 ); the other two points on the second rectangle are (x3, y3), (x4, y4 ).
Output
Output For each case output the area of their intersected part in a single line. accurate up to 2 decimal places.
Sample Input
1.00 1.00 3.00 3.00 2.00 2.00 4.00
5.00 5.00 13.00 13.00 4.00 4.00 12.50
Sample Output
1.00
56.25
The area of the intersection rectangle
Method 1: coordinates in the upper left corner of the rectangle and those in the lower right corner of the rectangle
Import java. io. *; import java. text. decimalFormat; import java. util. *; public class Main {public static void main (String [] args) {consumer SC = new consumer (new BufferedInputStream (System. in); while (SC. hasNextDouble () {double s = 0; double res [] = new double [4]; double res1 [] = new double [4]; double res2 [] = new double [4]; double point1 [] = new double [4]; double point2 [] = new double [4]; for (int I = 0; I <point1.length; I ++) {point1 [I] = SC. nextDouble () ;}for (int I = 0; I <point2.length; I ++) {point2 [I] = SC. nextDouble ();} // coordinate res1 [0] = Math in the upper left corner of the first rectangle. min (point1 [0], point1 [2]); // X1res1 [1] = Math. max (point1 [1], point1 [3]); // Y1 // coordinate res1 [2] = Math in the bottom right corner of the first rectangle. max (point1 [0], point1 [2]); // x2res1 [3] = Math. min (point1 [1], point1 [3]); // y2 // coordinate res2 [0] = Math in the upper left corner of the second rectangle. min (point2 [0], point2 [2]); // x1res2 [1] = Math. max (point2 [1], point2 [3]); // y1 // The coordinate res2 [2] = Math in the lower right corner of the second rectangle. max (point2 [0], point2 [2]); // x2res2 [3] = Math. min (point2 [1], point2 [3]); // y2 // if the rectangle is remerged and separated, s = 0 if (res2 [0]> = res1 [2] | res2 [2] <= res1 [0] | res2 [3]> = res1 [1] | res2 [1] <= res1 [3]) {s = 0;} else {// The coordinate res [0] = Math. max (res1 [0], res2 [0]); res [1] = Math. min (res1 [1], res2 [1]); res [2] = Math. min (res1 [2], res2 [2]); res [3] = Math. max (res1 [3], res2 [3]); s = (res [2]-res [0]) * (res [1]-res [3]);} decimalFormat fo = new DecimalFormat ("0.00"); System. out. println (fo. format (s ));}}}
Method 2: Calculate the coordinates in the lower left corner of the rectangle and the coordinates in the upper right corner.
Import java. io. *; import java. text. decimalFormat; import java. util. *; public class Main {public static void main (String [] args) {consumer SC = new consumer (new BufferedInputStream (System. in); while (SC. hasNextDouble () {double s = 0; double x1 = SC. nextDouble (); double y1 = SC. nextDouble (); double x2 = SC. nextDouble (); double y2 = SC. nextDouble (); double x3 = SC. nextDouble (); double y3 = SC. nextDouble (); double x4 = SC. nextDouble (); double y4 = SC. nextDouble (); // Coordinate double xx1 = Math. min (x1, x2); // X1double yy1 = Math. min (y1, y2); // Y1 // Coordinate double xx2 = Math in the upper right corner of the first rectangle. max (x1, x2); // x2double yy2 = Math. max (y1, y2); // The Coordinate double xx3 = Math in the lower left corner of the second rectangle. min (x3, x4); // X1double yy3 = Math. min (y3, y4); // Y1 // Coordinate double xx4 = Math in the upper right corner of the second rectangle. max (x3, x4); // x2double yy4 = Math. max (y3, y4 ); // y2 // if the rectangle is combined and separated, s = 0 if (xx3> = xx2 | xx4 <= xx1 | yy3> = yy2 | yy1> = yy4) {s = 0;} else {// The Coordinate double a = Math. max (xx1, xx3); double B = Math. min (xx2, xx4); double c = Math. max (yy1, yy3); double d = Math. min (yy2, yy4); s = (B-a) * (d-c);} DecimalFormat fo = new DecimalFormat ("0.00"); System. out. println (fo. format (s ));}}}