hdu_problem_2056_ Rectangles

题目大意:给两个矩形对角线上的点(主对角线或者副对角线),求两个矩形重叠部分的面积。
输入:每一行都是八个数字,对应4个点。
输出:输出重叠的面积,精确到小数点后两位。
方法:在这里插入图片描述

/*
*
*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
*
*
*Author
*seeyou
*
*
*Source
*校庆杯Warm Up
*
*
*Recommend
*linle
*
*/
#include<iostream>
using namespace std;
//            0  1  2  3  4  5  6  7 
double a[8];// x1,y1,x2,y2,x3,y3,x4,y4
double point[4];
int i;
int main() {
 while (~scanf_s("%lf%lf%lf%lf%lf%lf%lf%lf", &a[0],&a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])){
  if (a[0] > a[2]) swap(a[0], a[2]);
  if (a[1] > a[3]) swap(a[1], a[3]);
  if (a[4] > a[6]) swap(a[4], a[6]);
  if (a[5] > a[7]) swap(a[5], a[7]);
  point[0] = a[0] > a[4] ? a[0] : a[4];
  point[1] = a[1] > a[5] ? a[1] : a[5];
  point[2] = a[2] < a[6] ? a[2] : a[6];
  point[3] = a[3] < a[7] ? a[3] : a[7];
  if (point[0] > point[2] || point[1] > point[3]) printf("0.00\n");
  else printf("%.2lf\n", (point[2] - point[0])*(point[3] - point[1]));
 }
 system("pause");
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章