Description
平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。
Input
输入仅包含两行,每行描述一个矩形。
在每行中,给出矩形的一对相对顶点的坐标,每个点的坐标都用两个绝对值不超过10^7的实数表示。
Output
输出仅包含一个实数,为交的面积,保留到小数后两位。
Sample Input
1 1 3 3
2 2 4 4
Sample Output
1.00
#include <cstdio>
#include <iostream>
#include <cmath>
#include <
string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
#define eps 1e-9
const int inf =
0x3f3f3f3f;
const int mod = 1e9+
7;
struct node
{
double x1, x2, y1, y2;
}matrix[10];
int main()
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &matrix[
0].x1, &matrix[
0].y1, &matrix[
0].x2, &matrix[
0].y2, &matrix[
1].x1, &matrix[
1].y1, &matrix[
1].x2, &matrix[
1].y2);
double max_x1 = max(min(matrix[
0].x1, matrix[
0].x2), min(matrix[
1].x1, matrix[
1].x2));
///交集区域的左下角
double max_y1 = max(min(matrix[
0].y1, matrix[
0].y2), min(matrix[
1].y1, matrix[
1].y2));
///交集区域的左下角
double min_x1 = min(max(matrix[
0].x1, matrix[
0].x2), max(matrix[
1].x1, matrix[
1].x2));
///交集区域的右上角
double min_y1 = min(max(matrix[
0].y1, matrix[
0].y2), max(matrix[
1].y1, matrix[
1].y2));
///交集区域的右上角
if(min_x1 > max_x1 && min_y1 >
max_y1)
printf("%.2f\n", (min_x1 - max_x1) * (min_y1 -
max_y1));
else///如果两个矩形无交集
printf(
"0.00\n");
return 0;
}
转载于:https://www.cnblogs.com/RootVount/p/11342202.html
相关资源:JAVA上百实例源码以及开源项目