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

來源:互聯網
上載者:User
Rectangles

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

Total Submission(s): 11631 Accepted Submission(s): 3716Problem DescriptionGiven 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 .

InputInput 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).

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

Sample Output

1.0056.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]);//X1res1[1]=Math.max(point1[1], point1[3]);//Y1//第一個矩形右下角的座標res1[2]=Math.max(point1[0], point1[2]);//x2res1[3]=Math.min(point1[1], point1[3]);//y2//  第二個矩形左上方的座標res2[0]=Math.min(point2[0], point2[2]);//x1res2[1]=Math.max(point2[1], point2[3]);//y1// 第二個矩形右下角的座標res2[2]=Math.max(point2[0], point2[2]);//x2res2[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);//X1double yy1=Math.min(y1,y2);//Y1//第一個矩形右上方的座標double xx2=Math.max(x1,x2);//x2double yy2=Math.max(y1,y2);//y2//第二個矩形的左下角的座標double xx3=Math.min(x3,x4);//X1double yy3=Math.min(y3,y4);//Y1//第二個矩形右上方的座標double xx4=Math.max(x3,x4);//x2double 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));}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.