【题解】- hdu Rectangles【矩形相交求面积问题】

Rectangles

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,x2,x3,x4 y1,y2,y3,y4的大小

再判断是否相交

如果相交则进行排序中间的连个数就是相交的两个坐标

求得相交的宽和长即可求出相交的面积是多少

AC代码
#include <stdio.h>
int main(void)
{
    double x[10], y[10], t;
    int i, j;
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x[1],&y[1],&x[2],&y[2],&x[3],&y[3],&x[4],&y[4]) != EOF){
        if(x[2]<x[1]){  //保证x2 > x1;
            t = x[2];
            x[2] = x[1];
            x[1] = t;
        }
        if(x[4]<x[3]){  //保证x4 > x3;
            t = x[4];
            x[4] = x[3];
            x[3] = t;
        }
        if(y[2]<y[1]){   //保证y2 > y1;
            t = y[2];
            y[2] = y[1];
            y[1] = t;
        }
        if(y[4]<y[3]){   //保证y4 > y3;
            t = y[4];
            y[4] = y[3];
            y[3] = t;
        }
        if(x[1]<x[4]&&x[2]>x[3]&&y[1]<y[4]&&y[2]>y[3]){  //如果相交
            for(i = 1; i < 5; i ++){
                for(j = 4; j > i; j--){
                    if(x[i] > x[j]){
                        t = x[i];
                        x[i] = x[j];
                        x[j] = t;
                    }
                }
            }
            for(i = 1; i < 5; i ++){
                for(j = 4; j > i; j--){
                    if(y[i] > y[j]){
                        t = y[i];
                        y[i] = y[j];
                        y[j] = t;
                    }
                }
            }
            printf("%.2f\n",(x[3] - x[2]) * (y[3] - y[2])); //始终取这四个数从小到大排的中间两个数;
        }
        else printf("0.00\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值