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;
}
时间: 2024-08-08 13:59:11

HDU2056 Rectangles【水题】【相交面积】的相关文章

HDU 2056 Rectangles(计算相交面积)

Rectangles Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20183    Accepted Submission(s): 6537 Problem Description Given two rectangles and the coordinates of two points on the diagonals of e

POJ 1269 Intersecting Lines(线段相交,水题)

Intersecting Lines 大意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:线段相交判断.求交点的水题,没什么好说的. struct Point{ double x, y; } ; struct Line{ Point a, b; } A, B; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x); } bool

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

NYOJ 1186 心理阴影(两个圆环的相交面积)

心理阴影 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述       自从shanghai reginal后,zkc学长的心理就有了阴影.什么不敢水题,不敢gj,不敢放松...(各种不敢).同样的你们的zsj学长也是这样的.并且他们的心理阴影是一样一样的. 已知一个人的心理阴影为一个环形,那么求你们的zkc学长和zjs学长站在一起的时候的心理阴影的重叠面积. 输入 T组数据 r,R表示是内圆半径和外圆半径. x1,y1 zkc学长站的位置(抽象为一个点) x2,y2

HDU 5135 Little Zu Chongzhi&#39;s Triangles(简单水题)

题目链接: 戳我 题目大意: 给一堆 木棍,用这些木棍组成三角形,要组成的所有的三角形的面积和最大,不一定要用完所有的木棍. 样例解释: 3 //三个棍子 1 1 20   // 每个棍子的长度,自然,这三个棍子不可能组成三角形,故输出 0.00 7          // 7个棍子 3 4 5 3 4 5 90 // 组成两个三角形(3, 3 4)和(4, 4, 5),面积和即为13.64 0   // 输出 0 退出 解题思路: 本来不想写题解的,因为太水了,,,,,,, 可是看到 谷歌搜出

1283 最小周长(水题)

1283 最小周长 题目来源: Codility 一个矩形的面积为S,已知该矩形的边长都是整数,求所有满足条件的矩形中,周长的最小值.例如:S = 24,那么有{1 24} {2 12} {3 8} {4 6}这4种矩形,其中{4 6}的周长最小,为20. Input 输入1个数S(1 <= S <= 10^9). Output 输出最小周长. Input示例 24 Output示例 20 虽然是一道简单的水题,但是通过比较别人的代码和自己的代码,还是学到了一些知识 别人的代码 15ms 20

HDOJ Intersection 5120【环相交面积】

Intersection Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 949    Accepted Submission(s): 360 Problem Description Matt is a big fan of logo design. Recently he falls in love with logo made

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

HDU 3264 Open-air shopping malls(圆相交面积+二分)

HDU 3264 Open-air shopping malls(圆相交面积+二分) ACM 题目地址:HDU 3264 Open-air shopping malls 题意: 给出一些圆,选择其中一个圆的圆心为圆心,然后画一个大圆,要求大圆最少覆盖每个圆的一半面积.求最小面积. 分析: 枚举每个点,用二分求出需要的圆,更新最小值即可. 其中用到了圆相交面积,可以参考这题: POJ 2546 Circular Area(两个圆相交面积) 代码: /* * Author: illuz <iillu