程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 2056 Rectangles (求相交矩形的面積)

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

編輯:C++入門知識

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<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));
		}
	}

}

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

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));
		}
	}

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved