HDU 2056 Rectangles (求相交矩形的面积)

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 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50

Sample Output
 
   
1.00 56.25


题目大意  求相交矩形的面积


方法一:   矩形左上角 的坐标和矩形右下角的坐标

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(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=res1[2]||res2[2]<=res1[0]||res2[3]>=res1[1]||res2[1]<=res1[3]){
				s=0;
			}
			else{
				//否者矩形相交的 坐标
				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));
		}
	}

}



 

方法二   求矩形 左下角的坐标和右上角的坐标


import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(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();
			//第一个矩形左下角的坐标
			double xx1=Math.min(x1,x2);//X1
			double yy1=Math.min(y1,y2);//Y1
			//第一个矩形右上角的坐标
			double xx2=Math.max(x1,x2);//x2
			double yy2=Math.max(y1,y2);//y2
			//第二个矩形的左下角的坐标
			double xx3=Math.min(x3,x4);//X1
			double yy3=Math.min(y3,y4);//Y1
			//第二个矩形右上角的坐标
			double xx4=Math.max(x3,x4);//x2
			double yy4=Math.max(y3,y4);//y2
			
			// 如果矩形重合和相离  s=0  
			if(xx3>=xx2||xx4<=xx1||yy3>=yy2||yy1>=yy4){
				s=0;
			}
			else{
				//否者矩形相交的 坐标
				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));
		}
	}

}


你可能感兴趣的:(ACM算法之美)