两矩形公共面积问题

Rectangles

TimeLimit: 1000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 17721    Accepted Submission(s): 5702

Problem Description

Given two rectangles and thecoordinates of two points on the diagonals of each rectangle,you have tocalculate the area of the intersected part of two rectangles. its sides areparallel to OX and OY .

 

 

Input

Input The first line of input is 8positive numbers which indicate the coordinates of four points that must be oneach diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the twopoints on the first rectangle are(x1,y1),(x2,y2);the other two points on thesecond rectangle are (x3,y3),(x4,y4).

 

 

Output

Output For each case output the area oftheir intersected part in a single line.accurate up to 2 decimal places.

 

 

Sample Input

1.001.00 3.00 3.00 2.00 2.00 4.00 4.00

5.005.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,当我们在输入的时候,并不能保证x1小于x2,y1小于y2,x3小于x4,y3小于y4,正因为如此,才给我们判断这两个矩形是否有公共面积带来了诸多麻烦,因为这样我们得考虑很多种情况,例如(我以对角线的方式表示):

以上给出的4种图方便读者对比,我画的时候并没有让其相交,但是相交的时候对角线的情况和这4幅图是完全一样的,大家想一想,既然我们并不知道将给出的对角线的坐标是哪一种情况,但是我们又要简单的完成这一题,为何我们不在判断是否相交之前将坐标统一成便于解题的形式了?接下来给大家说说怎么统一了。

      这一题,统一坐标有很多种方法,在这里我将坐标统一成总是由图1的形式出现,即两个矩形的对角线都是斜向右上的,下面由代码的形式给出方法:

if(x1 >x2)   t = x1, x1 = x2, x2 = t;

if(y1 >y2)   t = y1, y1 = y2, y2 = t;

 if(x3 > x4) t = x3, x3  = x4,x4 = t;

 if(y3 > y4)  t = y3, y3 = y4, y4 = t;

由这4个式子就完成了将所有给出的矩形的对角线都像图1那样斜向右上了,接下来,我们要做的就是讨论是否相交问题了,大家在一次想一想,我们给出的两个矩形的相对位置现在还是不知道啊,要判断他们是否相交,是不是还是很麻烦啊,对,是还很比较麻烦,我也嫌它麻烦,所以,我又想,既然在同一矩形中x1小于x2,x3小于x4,y1小于y2,y3小于y4,那我们是不是可以用两个矩形中相对较小的横坐标中较大的横坐标了和相对较大的横坐标中较较小的横坐标进行比较,以及两个矩形中相对较小的纵坐标中较大的纵坐标了和两个矩形中相对较大的纵坐标中较小的纵坐标进行比较了?是的,接下来的思路就是这样的,如果两个矩形中较小的横、纵坐标中大的横、纵坐标大于两个矩形中较大的横、纵坐标中小的横、纵坐标,它们就没有公共面积,同样用代码来解释这一段话:

x1 = max(x1,x3);    x2= min(x2,x4);

y1 = max(y1,y3);   y2 = min(y2,y4);

if(x1 > x2 ||y1 > y2)  这两个矩形没有公共面积!

else  s =(x2 - x1) * (y2 – y1);

到这里,这一道的基本就完成了,下面我将会给出完整的代码,

【代码】

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<stdio.h>

using namespacestd;

int main()

{

    double x1, y1, x2, y2, x3, y3, x4, y4;

    while(cin >> x1 >> y1 >>x2 >> y2 >> x3 >> y3 >> x4 >> y4)

    {

        double s = 0.00, t1 =0.00, t2 = 0.00,t3 =0, t4 = 0.00;

        if(x1 > x2)

        {

            t1 = x1;

            x1 = x2;

            x2 = t1;

        }

        if(y1 > y2)

        {

            t2 = y1;

            y1 = y2;

            y2 = t2;

        }

        if(x3 > x4)

        {

            t3 = x3;

            x3 = x4;

            x4 = t3;

        }

        if(y3 > y4)

        {

            t4 = y3;

            y3 = y4;

            y4 = t4;

        }

        x1 = max(x1, x3);

        y1 = max(y1, y3);

        x2 = min(x2, x4);

        y2 = min(y2, y4);

        s = x1 > x2 || y1 > y2 ? 0 : (x2- x1) * (y2 - y1);

        cout << fixed <<setfill('0') << setprecision(2) << s <<endl;

    }

    return 0;

}

你可能感兴趣的:(两矩形公共面积问题)