基础练习 矩形面积交

问题描述

  平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。

输入格式

  输入仅包含两行,每行描述一个矩形。 在每行中,给出矩形的一对相对顶点的坐标,每个点的坐标都用两个绝对值不超过10^7的实数表示。

输出格式

  输出仅包含一个实数,为交的面积,保留到小数后两位。

样例输入

1 1 3 3
2 2 4 4

样例输出

1.00

问题分析

  将已知四个点的横坐标和纵坐标进行排序, 分别取中间两个值作为相交矩形的宽和高。

测试代码

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <algorithm>
 4 
 5 using std::cin;
 6 using std::cout;
 7 using std::endl;
 8 using std::max;
 9 using std::min;
10 using std::fixed;
11 using std::setprecision;
12 
13 struct Point
14 {
15     double nX;
16     double nY;
17 };
18 
19 int main(void)
20 {
21     Point _ptA, _ptB;
22     Point _ptC, _ptD;
23     cin >> _ptA.nX >> _ptA.nY;
24     cin >> _ptB.nX >> _ptB.nY;
25     cin >> _ptC.nX >> _ptC.nY;
26     cin >> _ptD.nX >> _ptD.nY;
27     
28     double _nInterAX = max(min(_ptA.nX, _ptB.nX), min(_ptC.nX, _ptD.nX));
29     double _nInterAY = max(min(_ptA.nY, _ptB.nY), min(_ptC.nY, _ptD.nY));
30     double _nInterBX = min(max(_ptA.nX, _ptB.nX), max(_ptC.nX, _ptD.nX));
31     double _nInterBY = min(max(_ptA.nY, _ptB.nY), max(_ptC.nY, _ptD.nY));
32 
33     double _nArea = 0;
34     if (_nInterBX > _nInterAX && _nInterBY > _nInterAY)
35     {
36         _nArea = (_nInterBX - _nInterAX) * (_nInterBY - _nInterAY);
37     }
38     
39     cout << fixed << setprecision(2) << _nArea << endl;
40     
41     return 0;
42 }

 

posted @ 2017-10-26 16:10  新生代黑马  阅读(260)  评论(0编辑  收藏  举报