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 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...
随机推荐
- Linux中国开源社区停止运营
layout: post title: "Linux 中国开源社区停止运营" tags: - "Linux" 昨天看到老王发的公众号文章,Linux中国开源社区 ...
- 小结_第一个Java程序
总结: 1. Java程序的编写与执行: 步骤1: 编写. 在后缀名为.java的文件中编写Java代码,该文件称为源文件 步骤2: 编译. 针对后缀名为.java源文件进行编译,生成字节码文件. 格 ...
- 小知识:使用MOS下载Oracle介质快速参考
之前对选Release.Patch Set.PSU都有专门的文档,现在早已简化,针对这些以及之后RU.RUR等都包含在MOS文档:2118136.2 Assistant: Download Refer ...
- 硬件、OS、数据库各种兼容性傻傻分不清?
服务器硬件型号.OS操作系统版本.Oracle数据库版本.. 各种型号各种版本,排列组合的话也是N多种组合,如何确认这些可以兼容并且得到官方认证呢? 其实很简单,各司其职,各自去确认各自的. 我们知道 ...
- ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记
任务9:客户端集成IdentityServer 新建 API 项目 dotnet new webapi --name ClientCredentialApi 控制器添加验证 using Microso ...
- Linux-crontab的使用
一.什么是crontab?crontab 是有cron (crond) 这个系统服务来控制的,cron服务是linux的内置服务,类似于window下的计划任务,但它不会开机自动启动 二.如何使用?c ...
- Linux-CentOS7登录页面出现Hint: caps lock on,输入大小写字母反了(大小写反转问题)
问题描述:虚拟机CentOS7,输入大小写字母反了,开启capslock的时候变成小写字母了,关闭则变成大写了... 解决办法:只需要执行:setleds +caps 或 setleds -caps ...
- 压缩软件7-Zip的简单使用
简介及下载地址 7-Zip是一款开源免费的压缩软件,支持windows系统及Linux系统,压缩后文件扩展名默认为.7z后缀. 网址: https://www.7-zip.org/ 截图如下: 也 ...
- JS leetcode 杨辉三角 超详细题解分析
壹 ❀ 引 刷leetcode的第四天,原题出处为杨辉三角,题目描述如下: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. ...
- NC51178 没有上司的舞会
题目链接 题目 题目描述 Ural大学有N名职员,编号为1~N. 他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司. 每个职员有一个快乐指数,用整数 \(Hi\) 给出,其中 \(1\le ...