hdu-2056-Rectangles

来源:互联网 发布:cloudzoom.js 编辑:程序博客网 时间:2024/06/05 19:06

Rectangles

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25081    Accepted Submission(s): 8163

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

题意:

给出俩个矩形的顶点(对角线上的)。也就是说一个矩形,你知道俩个顶点,这俩个点在一条对角线上。那么求给出的俩个矩形的面积。

解题思路:

俩个矩形,有俩种情况,一:相交,二:不相交。感觉是废话。

这里写图片描述

不相交有俩种情况,如图,那么给x,y坐标从大到小排序,如果排序后前俩个横坐标是一个矩形的,或者纵坐标前俩个是一个矩形,那么他们肯定不相交。想想为什么?

这里写图片描述

可以看出重合的部分就是排序后中间的俩个数据。那么求面积就简单了。
# include <cstdio># include <algorithm>using namespace std;struct node{    int id;    double num;};bool cmp(node a,node b){    return a.num < b.num;}int main(){    node x[6],y[6];    while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x[0].num,&y[0].num,&x[1].num,&y[1].num,&x[2].num,&y[2].num,&x[3].num,&y[3].num) != EOF)    {        x[0].id = x[1].id = y[0].id = y[1].id = 1;        x[3].id = x[2].id = y[3].id = y[2].id = 2;        sort(x,x+4,cmp);        sort(y,y+4,cmp);        if(x[1].id == x[0].id || y[1].id == y[0].id)        {            printf("0.00\n");            continue;        }        printf("%.2lf\n",(x[2].num-x[1].num)*(y[2].num-y[1].num));    }    return 0;}
原创粉丝点击