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 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...
随机推荐
- 飞桨paddlespeech语音唤醒推理C INT8 定点实现
前面的文章(飞桨paddlespeech语音唤醒推理C定点实现)讲了INT16的定点实现.因为目前商用的语音唤醒方案推理几乎都是INT8的定点实现,于是我又做了INT8的定点实现. 实现前做了一番调研 ...
- ChatGPT 火了,一大批开源平替也来了
ChatGPT 凭一己之力让全球为 AI 沸腾,引发了科技巨头之间的聊天机器人军备竞赛. 众所周知,ChatGPT 的背后技术是 GPT(Generative Pre-trained Transf ...
- workman在线五子棋
一.下载安装workman,地址:https://github.com/walkor/workerman composer require workerman/workerman 二.cd到worke ...
- Vulkan学习苦旅01:最初的相遇(学习路线、参考资料与环境配置)
提示:博主本人也在努力学习Vulkan中,文中可能有写错的地方,敬请大家批评指正. 这个世界只有两种人:会Vulkan的和不会Vulkan的,大概不存在"只会一点"的中间状态.学习 ...
- Label的背景色
Label的背景色是 color属性,但是这个属性是 必须 Transparent 为 false的时候 才生效,否则不生效
- Java并发编程-CompletableFuture(下)
大家好,我是小高先生,书接上文,我们继续来学习CompletableFuture.上文我们讲了基础装Future是如何升级为神装CompletableFuture以及如何购买CompletableFu ...
- NC50428 数星星 Stars
题目链接 题目 题目描述 天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标.如果一个星星的左下方(包含正左和正下)有k颗星星,就说这颗星星是k级的. 例如,上图中星星5是3级的(1,2,4在 ...
- NC19996 [HAOI2015]树上染色
题目链接 题目 题目描述 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色. 将所有点染色后,你会获得黑点两两之间的 ...
- thymeleaf利用fragment解决html页面间获取context-path问题
问题说明 我使用spring boot+thymeleaf做了个项目,那前台页面都是html,里面有各种api调用和路径跳转. 大家都知道这些路径不能写死,为保证任何情况下路径的正确性,一般都是这种格 ...
- Java集合框架学习(三) TreeSet详解
TreeSet介绍 1.TreeSet是SortedSet接口的唯一实现类,TreeSet可以确保集合元素处于排序状态. 2.向TreeSet中加入的应该是同一个类的对象. 3.TreeSet判断两个 ...