HDU 2056: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 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).
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
输出
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.
1.00
56.25
五、思路
已知,当给出一个矩形两个对角点的坐标\((x_1,y_1),(x_2,y_2)\)可确定一个矩形的坐标和大小,且该矩形的两边必然与矩形的两边平行

注:已知对角点A(0,0),B(1,1)即可知道矩形的坐标与大小
题目中给出了两个矩形的两个对角点坐标,要求相交部分的大小。显然,如果有相交部分,则该相交部分必为与坐标轴平行的矩形,故题目可转化为求该矩形的两个对角点。

注:要求相交部分,只需求点I,J即可
如已知两个矩形的对角点坐标\((x_G,y_G),(x_H,y_H),(x_A,y_A),(x_B,y_B)\),考虑对角点\((x_I,y_I),(x_J,y_J)\)的性质(以x坐标为例)
- 性质一:在数值上,由于求的是交集,\(x_I,x_J\)必为\(x_G,x_H,x_A,x_B\)的中间数
- 性质二:由于\(I(x_I,y_I),G(x_G,y_G)\)两点同时在两个矩形上,故有\(x_G<=x_I<=x_H,x_A<=x_I<=x_B\)(\(x_G\)类似)
六、code
#include <bits/stdc++.h>
#define PI 3.1415927
using namespace std;
typedef double elemType;
typedef struct{
double x,y;
}Point;
bool contain(double num,double range1,double range2){
if(range1>range2){
swap(range1,range2);
}
return num>=range1&&num<=range2?true:false;
}
int main()
{
Point p[2][2]; //二维数组:第一维表示第几个矩形,第二位表示第几个点
double x[4],y[4];
while(cin >> p[0][0].x >> p[0][0].y >> p[0][1].x >> p[0][1].y >> p[1][0].x >> p[1][0].y >> p[1][1].x >> p[1][1].y){
//根据性质一:选取可能的交点:交点必然在内部
int num=0;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
x[num]=p[i][j].x;
y[num]=p[i][j].y;
num++;
}
}
sort(x,x+4);
sort(y,y+4);
Point ans[2];
ans[0].x=x[1];
ans[1].x=x[2];
ans[0].y=y[1];
ans[1].y=y[2];
//根据性质二:判断两点是否有效
bool flag=1;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
if(!contain(ans[i].x,p[j][0].x,p[j][1].x)||!contain(ans[i].y,p[j][0].y,p[j][1].y)){
flag=0;
}
}
}
printf("%.2lf\n",flag?(ans[1].y-ans[0].y)*(ans[1].x-ans[0].x):0);
}
return 0;
}
HDU 2056:Rectangles(两个矩形交点的性质)的更多相关文章
- HDU 2056 Rectangles
Rectangles Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDOJ 2056 Rectangles
Problem Description Given two rectangles and the coordinates of two points on the diagonals of each ...
- Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)
Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box) function DecideOberlap(BBox_x1, BBox_y1, BBox_x2, ...
- PHP判断两个矩形是否相交
<?php $s = is_rect_intersect(1,2,1,2,4,5,0,3); var_dump($s); /* 如果两个矩形相交,那么矩形A B的中心点和矩形的边长是有一定关系的 ...
- Python 实现两个矩形重合面积
计算两个矩形的重合面积 import math x1, y1, x2, y2 = input().split(" ") x1, y1, x2, y2=int(x1), int(y1 ...
- 2018-12-24-win10-uwp-求两个矩形相连的几何
title author date CreateTime categories win10 uwp 求两个矩形相连的几何 lindexi 2018-12-24 20:51:49 +0800 2018- ...
- [cocos2d-x]判断两个矩形是否有交叉区域
bool Rect::intersectsRect(const Rect& rect) const { return !( getMaxX() < rect.getMinX() || r ...
- Overlapping rectangles判断两个矩形是否重叠的问题 C++
Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by pro ...
- Codeforces Round #587 (Div. 3) C题 【判断两个矩形是否完全覆盖一个矩形问题】 {补题 [差点上分系列]}
C. White Sheet There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle ...
- [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...
随机推荐
- 外部文件使用django的models
#外部文件使用django的models,需要配置django环境 import os if __name__ == '__main__': os.environ.setdefault("D ...
- idea启动springboot项目报错java.lang.ClassNotFoundException: com.gctl.bpm.GctlBpmApplication解决方案
有时候父子工程改造springboot项目时会报错java.lang.ClassNotFoundException: com.gctl.bpm.GctlBpmApplication如下图所示 此时不要 ...
- 阿里云数仓Dataworks数据导出到文件step by step
背景 假设你在阿里云上Dataworks的空间space下有一个表table_A,想要把它的数据导出到文件以供后续使用,但是数据量又很多,从浏览器复制不太现实.阿里云提供了Java和Python版本的 ...
- SQLWorkbench使用自定义JDBC驱动连接数据库
一.Windows上使用SQLWorkbench 1. 添加CloudDB的驱动,点击"Manage Driver". Name : Fandatsys-CDB sample U ...
- [JVM]逃逸分析
逃逸分析 JVM的内存分配策略 首先回顾一下JVM的内存分配策略. JVM的内存包括方法区.堆.虚拟机栈.本地方法栈.程序计数器.一般情况下JVM运行时的数据都是存在栈和堆上的.栈用来存放一些基本变量 ...
- Linux 中竖线“|”与双竖线“||”的意思
linux中竖线'|',双竖线'||',&和&&的意思 对于初学者来说这几个意思可能只知道其中几个的意思,下面我们来看一下. 1.竖线'|' ,在linux中是作为管道符的,将 ...
- CentOS在无网络环境下,用离线源yum安装软件
先说大致步骤: 1.前提假设:当前无网络的目标服务器是A,我们需要先准备一台服务器B: 2.在B上面用yum先把软件安装完成. 3.然后用createrepo将B中的包拷贝出来,并传到A上(用U盘或者 ...
- JetBrains全系列软件激活教程激活码以及JetBrains系列软件汉化包
最新激活方式 如果安装过无限试用.修改过 hosts 请先卸载干净旧版本再安装,否需无法激活! 以 WebStorm 为例 JetBrains官网 https://www.jetbrains.com/ ...
- std::string std::wstring char w_char 内部中文编码
最近在处理一个字符串转码问题,故记录一下过程 该需求是外部 sdk 的一个 api 需要一个 char* 字符串路径入参,我以往是将宽字符串转为 UTF8 后再传给 sdk 这次这个 api 似乎不接 ...
- error C2039: "function": 不是 "std" 的成员的解决方法
这个错误通过某度没找到合适的解决方案,故记录下来 其实如果使用 google 搜索错误的英文关键词,大概第一条就是解决问题的链接 Large number of "'function' is ...