Determine overlapping rectangles
On-Site Question 3 - SOLUTION
Problem
Given two rectangles, determine if they overlap. The rectangles are defined as a Dictionary, for example:
r1 = {
# x and y coordinates of the bottom-left corner of the rectangle
'x': 2 , 'y': 4,
# Width and Height of rectangle
'w':5,'h':12}
If the rectangles do overlap, return the dictionary which describes the overlapping section
Requirements
Make sure the dictionary you output is in the same form as the input.
Feel free to use an IDE for the code, but make sure you use paper/pencil or whiteboard to draw out your plan and logic
Solution
This is a problem where it helps a lot to draw out your thinking. There are a few things we will need to think about:
- How can we determine an intersection?
- What if a rectangle is fully inside another rectangle?
- What if there is no intersection, but the rectangles share an edge?
The key to solving this problem is to break it up in to sub-problems. We can split up the problem into an x-axis problem and a y-axis problem.
We will create a function that can detect overlap in 1 dimension. Then we will split the rectangles into x and width, and y and height components. We can then determine that if there is overlap on both dimensions, then the rectangles themselves intersect!
In order to understand the calc_overlap function, draw out two flat lines and follow along with the function and notice how it detects an overlap!
Let's begin by creating a general function to detect overlap in a single dimension:
def calc_overlap(coor1,dim1,coor2,dim2):
"""
Takes in 2 coordinates and their length in that dimension
""" # Find greater of the two coordinates
# (this is either the point to the most right
# or the higher point, depending on the dimension) # The greater point would be the start of the overlap
greater = max(coor1,coor2) # The lower point is the end of the overlap
lower = min(coor1+dim1,coor2+dim2) # Return a tuple of Nones if there is no overlap if greater >= lower:
return (None,None) # Otherwise, get the overlap length
overlap = lower-greater return (greater,overlap)
Now let's use this function to detect if the rectangles overlap!
def calc_rect_overlap(r1,r2):
x_overlap, w_overlap = calc_overlap(r1['x'],r1['w'],r2['x'],r2['w'])
y_overlap, h_overlap = calc_overlap(r1['y'],r1['h'],r2['y'],r2['h'])
# If either returned None tuples, then there is no overlap!
if not w_overlap or not h_overlap:
print 'There was no overlap!'
return None
# Otherwise return the dictionary format of the overlapping rectangle
return { 'x':x_overlap,'y': y_overlap,'w':w_overlap,'h':h_overlap}
Our solution is O(1) for both time and space! Let's see it in action:
r1 = {'x': 2 , 'y': 4,'w':5,'h':12}
r2 = {'x': 1 , 'y': 5,'w':7,'h':14}
calc_rect_overlap(r1,r2)
{'h': 11, 'w': 5, 'x': 2, 'y': 5}
Make sure to review the answer and practice writing it out by hand!
Good Job!
Determine overlapping rectangles的更多相关文章
- Overlapping rectangles判断两个矩形是否重叠的问题 C++
Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by pro ...
- 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
There are nn rectangles on the plane. The problem is to find the area of the union of these rectangl ...
- 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles
2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...
- 计蒜客 Overlapping Rectangles (离散化)
题意: 给定一个坐标系, 给出n个矩形的左下角坐标(bx,by)和右上角坐标(tx,ty) , 求矩形覆盖的面积, 有些区域会被多个矩形覆盖, 但只用算一次. n <= 1000, 0 < ...
- 2017 icpc 南宁网络赛
2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
02Train Seats Reservation 问答 只看题面 33.87% 1000ms 131072K You are given a list of train stations, say ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 (B,F,L,M)
B. Train Seats Reservation You are given a list of train stations, say from the station 1 to the sta ...
- R绘图基础
一,布局 R绘图所占的区域,被分成两大部分,一是外围边距,一是绘图区域. 外围边距可使用par()函数中的oma来进行设置.比如oma=c(4,3,2,1),就是指外围边距分别为下边距:4行,左边距3 ...
随机推荐
- PHP闭包
# 提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它.声明一个匿名函数是这样: $func = function() { }; //带结束符 ...
- 前台框架vue.js中怎样嵌入 Echarts 组件?
目前常用的图标插件有 charts, Echarts, highcharts.这次将介绍 Echarts 在 Vue 项目中的应用. 一.安装插件 使用 cnpm 安装 Echarts cnpm i ...
- php缓存类
<?php /* * 缓存类 cache * 实 例: include( "cache.php" ); $cache = new cache(30); $cache-> ...
- as3 区别中文 英文 数字
1)英文a-z是65-90,A-Z是97-112 2)数字是0-9是,48-57 3)上万的都是中文字符 var str:String = "hello world! 你好世界! 88!&q ...
- python中关键字的总结
python中各种关键字的总结:用表格形式,解释关键字符号的作用和案例说明 关键字 ...
- UI5-文档-4.1-Hello World!
如你所知,SAPUI5是关于HTML5的.让我们开始构建第一个仅使用HTML的“Hello World”. Preview 浏览器显示文本“Hello World” Coding 你可以在此查看和下载 ...
- 快速可靠网络传输协议 KCP(转)
KCP 是一个快速可靠协议,能以比 TCP浪费10%-20%的带宽的代价,换取平均延迟降低30%-40%,且最大延迟降低三倍的传输效果.纯算法实现,并不负责底层协议(如UDP)的收发,需要使用者自己定 ...
- InitComponent的使用
网页中的数据,有些是不在网页上改变的,像一些个人信息,比如:头像,当前用户名,友情链接等等,每次请求该页面都要重新加载,这样很消耗服务器资源,会降低服务器的性能,这个时候我们可以把这些不变的信息,统一 ...
- list接口如何使用
1集合类,在java语言中的java.util包提供了一些集合类,这些集合类又被称作容器. 2区别集合类和数组.(1)数组的长度是固定的,集合的长度是可变的.(2)数组是用来存放基本数据类型的,集合是 ...
- CGBitmapContextCreate函数参数详解 以及在 ios7下变化
函数原型: CGContextRef CGBitmapContextCreate ( void *data, size_t width, size_t height, size_t ...