杭电OJ2056(求俩矩形相交的面积)

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

思路

这题题意是给你两个矩形的分别两个点,(x1,y1),(x2,y2)和(x3,y3),(x4,y4),让你求出其两者重叠的面积。

我们乍一想,肯定觉得很简单,但仔细一想就觉的,情况会特别多了
按大类分为:相离,相交和包含 三种情况。
细分就更多了,因为其给我们的点不是非常规范的,不是左下和右上。
然后当我们通过比较后,规范后,又有问题出现了,就是如何去找重叠面积矩阵的 长和宽。
通过解决好长和宽就可以求出重叠的面积了,别忘了保存两位小数。

基本步骤:

1.处理同一矩形中的左下点和右上点的坐标。
两个横坐标 相比 和 两个纵坐标 相比 , 从中进行交换。
2.找出的 长和宽 。
a.如果两个矩形是相离的话,那么直接输出 0.00
b.如果两个矩形是相交或包含,从中去找出重叠矩形的左下点和右上点的坐标
(1)a = x1
(2)b = x2
(3)c = y1
(4)d = y2
左下点 (a,c) 右上点 (b,d)
3.求重叠矩形的面积
S=(b-a)*(d-b)

#include<stdio.h>
#include<string.h>
int main()
{
    double k,a1,b1,a2,b2,c1,d1,c2,d2,a,b,c,d;
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a1,&b1,&a2,&b2,&c1,&d1,&c2,&d2)!=EOF)
    {
        if(a1>a2)k=a1,a1=a2,a2=k;
        if(b1>b2)k=b1,b1=b2,b2=k;
        if(c1>c2)k=c1,c1=c2,c2=k;
        if(d1>d2)k=d1,d1=d2,d2=k;
        if(a1>c2||a2<c1||b1>d2||b2<d1)//判断是否相离
            printf("0.00\n");
        else
        {
            if(a1<=c1)a=c1;
            else a=a1;
            if(a2<=c2)b=a2;
            else b=c2;
            if(b1<=d1)c=d1;
            else c=b1;
            if(b2<=d2)d=b2;
            else d=d2;
            printf("%.2lf\n",(b-a)*(d-c));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43328040/article/details/84707632