• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

杭电ACM 2056 Rectangles,求相交矩形的面积 Java实现

JAVA相关 水墨上仙 2100次浏览

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11637 Accepted Submission(s): 3717

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

代码一:矩形左上角&nbsp的坐标和矩形右下角的坐标

代码转自:http://blog.csdn.net/deng_hui_long/


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<point1.length;i++){
				point1[i]=sc.nextDouble();
			}
			for(int i=0;i<point2.length;i++){
				point2[i]=sc.nextDouble();
			}
			//  第一个矩形左上角的坐标
			res1[0]=Math.min(point1[0], point1[2]);//X1
			res1[1]=Math.max(point1[1], point1[3]);//Y1
			//第一个矩形右下角的坐标
			res1[2]=Math.max(point1[0], point1[2]);//x2
			res1[3]=Math.min(point1[1], point1[3]);//y2
			//  第二个矩形左上角的坐标
			res2[0]=Math.min(point2[0], point2[2]);//x1
			res2[1]=Math.max(point2[1], point2[3]);//y1
			// 第二个矩形右下角的坐标
			res2[2]=Math.max(point2[0], point2[2]);//x2
			res2[3]=Math.min(point2[1], point2[3]);//y2
			// 如果矩形重合和相离  s=0  
			if(res2[0]>=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));
		}
	}

}

代码2:求矩形&nbsp左下角的坐标和右上角的坐标

代码转自:http://blog.csdn.net/deng_hui_long/


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 2056 Rectangles,求相交矩形的面积 Java实现
喜欢 (0)
加载中……