Rectangle Area
2015-06-27 19:53
288 查看
Description:
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
View Code
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { Rectangle rect1(A,C,B,D); Rectangle rect2(E,G,F,H); int areaRect1 = (rect1.right-rect1.left)*(rect1.top-rect1.bottom); int areaRect2 = (rect2.right-rect2.left)*(rect2.top-rect2.bottom); int maxLeft = max(rect1.left, rect2.left); int minRight = min(rect1.right, rect2.right); int maxBottom = max (rect1.bottom, rect2.bottom); int minTop = min(rect1.top, rect2.top); int x = minRight-maxLeft; int y = minTop-maxBottom; if ( x > 0 && y > 0) return areaRect1+areaRect2-x*y; else return areaRect1+areaRect2; }
View Code
相关文章推荐
- 自定义Data Service Providers
- Java的3种Base64加密方法
- mongodb-java-driver基本用法
- 算法小白总结(三)-----分治法&&棋盘覆盖问题
- findViewById如果找不到控件
- 小贝_mysql学习准备
- DataTables选择多行
- 欢迎使用CSDN-markdown编辑器
- Facebook的ATOM Editor的底层Electron
- android菜鸟学习笔记7----android布局(二)
- linux常用命令的英文单词缩写
- 黑马程序员-OC语言-ios第一天笔记总结
- Linux图形界面与字符界面切换
- 思维改变生活之记忆
- poj 3352 Road Construction (无向图edge-BCC缩点)
- Hadoop优化 第一篇 : HDFS/MapReduce
- 十、C# 异常处理
- hihocoder题库1000
- Number of Islands (leetcode)
- 欢迎使用CSDN-markdown编辑器