首页 > 代码库 > HDU2056 Rectangles【水题】【相交面积】
HDU2056 Rectangles【水题】【相交面积】
Rectangles
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15823 Accepted Submission(s): 5061
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
题目大意:给你两个矩形对角线两端的坐标,输出这两个矩形的相交面积
思路:假设两个矩形相交,则相交的矩形面积横坐标为四个横坐标中间的
两个横坐标,纵坐标为四个纵坐标中间的两个纵坐标,然后计算面积。若
两个矩形不相交,则相交面积为0.00。
那么怎么判断是否相交呢。思路很简单,分别计算出矩形1和矩形2最小和
最大和横、纵坐标,若矩形1的最小横坐标>=矩形2的最大横坐标 或者
矩形1的最大横坐标<=矩形2的最小横坐标 则两个矩形不可能相交,同理,
纵坐标也是如此。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main() { double x[5],y[5],x1,x2,y1,y2; double Minx1,Minx2,Miny1,Miny2,Maxx1,Maxx2,Maxy1,Maxy2; while(cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]) { Minx1 = min(x[0],x[1]); Minx2 = min(x[2],x[3]); Miny1 = min(y[0],y[1]); Miny2 = min(y[2],y[3]); Maxx1 = max(x[0],x[1]); Maxx2 = max(x[2],x[3]); Maxy1 = max(y[0],y[1]); Maxy2 = max(y[2],y[3]); if(Minx1>=Maxx2||Maxx1<=Minx2||Miny1>=Maxy2||Maxy1<=Miny2) { printf("0.00\n"); continue; } sort(x,x+4); sort(y,y+4); x1 = x[1]; x2 = x[2]; y1 = y[1]; y2 = y[2]; double S = fabs((y1-y2)*(x1-x2)); printf("%.2lf\n",S); } return 0; }
HDU2056 Rectangles【水题】【相交面积】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。