HDU——2056Rectangles(几何计算)
来源:互联网 发布:知乎书店 编辑:程序博客网 时间:2024/06/05 16:15
Rectangles
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19243 Accepted Submission(s): 6233
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.005.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50
Sample Output
1.0056.25
这题首先把给的四个坐标分成两组
每组都要变成主对角线的顺序(即a↗b,c↗d)
可能的顺序共有四种:
1.主对角线,但是a,b顺序反的;
2.为副对角线但a在b左边;
3.为副对角线且a在b右边;
4.主对角线且a在b左边(正确顺序)
然后再总结下面积规律会发现若有重合面积总有
area=【min(b.x,d.x)-max(a.x,c.x)】*【min(d.y,b.y)-max(a.y,c.y)】且此式子也可以判断是否存在重合面积。若出现负数则不重合
代码:
#include<iostream>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;struct point {double x;double y;};inline void change(point &a,point &b){double t;point tp;if((a.x<b.x&&a.y>b.y)||(a.x>b.x&&a.y<b.y))//a↘b与b↘a转为主对角线,顺序先不变 {t=a.y;//这里暂时仅需y交换 a.y=b.y;b.y=t;}if(a.x>b.x&&a.y>b.y)//再将主对角线上的顺序变成↗的顺序 {tp=a;//整体交换 a=b;b=tp;}}int main(void){point a,b,c,d;while (cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y){change(a,b);change(c,d);if(min(b.x,d.x)<=max(a.x,c.x)||min(d.y,b.y)<=max(a.y,c.y))//两个多项式均不能为负数(=号包括了边重合) cout<<"0.00"<<endl;//此题大坑,0也要输出0.00。-.-|||else{double sum=(min(b.x,d.x)-max(a.x,c.x))*(min(d.y,b.y)-max(a.y,c.y));printf("%.2lf\n",sum);}}return 0;}
0 0
- HDU——2056Rectangles(几何计算)
- HDU 2056 Rectangles(计算相交面积)
- HDUOJ 2056 Rectangles (几何计算问题)
- hdu_2056 Rectangles (计算几何)
- Rectangles 简单几何计算
- HDU——2056——Rectangles
- hdoj 2056 Rectangles(矩形) 【几何】
- 【暑期基础3】D HDU 2056 Rectangles (几何)
- UVA 11930 - Rectangles(2-sat + 计算几何)
- HDU 5858 Hard problem——计算几何(微积分)
- 2056——Rectangles
- hdu 2876 (计算几何)
- hdu 5733 (计算几何)
- HDU-4823(计算几何)
- HDU-1071(计算几何)
- HDU 2056 Rectangles(矩形面积交)
- hdu 2056 Rectangles
- HDU 2056 Rectangles
- 百岁老人(有照片)
- Light OJ 1079 Just another Robbery (概率+背包)
- 线段树常见套路
- 1023. 组个最小数 (20)
- Elasticsearch进化史
- HDU——2056Rectangles(几何计算)
- PHP---APP笔记
- BNUOJ 51275 道路修建 Large(并查集)
- hdoj 5623 KK's Number 【dp】
- ElasticSearch中的一些基本概念
- HDU 2047 阿牛的EOF牛肉串
- 如何创建一个完整的C语言程序心得
- sql查询优化
- IoRegisterFsRegistrationChange routine