hdu2056 Rectangular

题目链接https://vjudge.net/problem/HDU-2056

题目如下

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

有两个边分别和OX轴,OY轴平行的矩形,那么他们的位置关系有三种

部分相交,完全包含,没有相交(题目说求相交部分面积,还得考虑没相交啊)

对于部分相交和完全包含的情况,可以画图知道相交部分是矩形

    相交部分平行OY轴的边长等于y1,y2,y3,y4这四个数中第二大的数和第三大的数的差

    同理,相交部分平行OX轴的边长等于x1,x2,x3,x4这四个数中第二大的数和第三大的数的差

没有相交的情况则输出0.00

代码如下

#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
double x[5],y[5];
int main()
{
	while(scanf("%lf%lf",&x[0],&y[0])!=EOF)
	{
		for(int i=1;i<4;i++)
			scanf("%lf%lf",&x[i],&y[i]);
		if(max(y[2],y[3])<=min(y[0],y[1])||min(y[2],y[3])>=max(y[0],y[1])||\
			max(x[2],x[3])<=min(x[0],x[1])||min(x[2],x[3])>=max(x[0],x[1]))
		{
			printf("0.00\n");
			continue;
		}
		sort(x,x+4);
		sort(y,y+4);
		double area;
		area=(x[2]-x[1])*(y[2]-y[1]);
		printf("%.2f\n",area);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41626975/article/details/82020070